美文网首页
Android usbCamera 拿到YUYV数据转为BMP

Android usbCamera 拿到YUYV数据转为BMP

作者: 怪咖小青年_56e8 | 来源:发表于2023-01-11 14:36 被阅读0次
    //点击事件
    public void onClickPhoto(View v) {
            int[] rgb = new int[Cmd.w_h*3];
            Bitmap bitmap = YUV2RGBABitmap2(Cmd.bufferlocal,rgb, 480, 640);
            saveImage(bitmap);
        }
    //此方法为yuyv数据 ,提取Y分量,只要黑白元素,转为BMP图片
    static public Bitmap YUV2RGBABitmap2(byte[] yuv, int[] rgb , int width, int height) {
    
            int y = 0;
    
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    int dex = (i * width + j)*2;
                        y = 0xff & yuv[dex];
                        rgb[i*width + j] = 0xff000000| y << 16 | y << 8 | y;
                }
            }
    
            Bitmap bmp = Bitmap.createBitmap(rgb,width, height, Bitmap.Config.ARGB_8888);
    
                return bmp;
            }
    public String saveImage(Bitmap bmp) {
    
            File appDir = new File(Environment.getExternalStorageDirectory(), "a");
            if (!appDir.exists()) {
                appDir.mkdir();
            }
            String fileName = System.currentTimeMillis() + ".bmp";
            File file = new File(appDir, fileName);
    
            try {
                FileOutputStream fos = new FileOutputStream(file);
    //            fos.write(bmp);
    //            writeBytesToFile(Cmd.bufferlocal,Cmd.bufferlocal.length,fos);
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return file.getAbsolutePath();
        }
    

    相关文章

      网友评论

          本文标题:Android usbCamera 拿到YUYV数据转为BMP

          本文链接:https://www.haomeiwen.com/subject/spwbcdtx.html