Android opencv byte[] 到 mat 到 byte[]

Android opencv byte[] to mat to byte[]

我的目标是在相机预览上添加一个叠加层,以找到书本边缘。为此,我在执行以下操作时重写了 onPreviewFrame:

public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        int width = parameters.getPreviewSize().width;
        int height = parameters.getPreviewSize().height;
        Mat mat = new Mat((int) (height*1.5), width, CvType.CV_8UC1);
        mat.put(0,0,data);
        byte[] bytes = new byte[(int) (height*width*1.5)];
        mat.get(0,0,bytes);

        if (!test) {        //to only do once
            File pictureFile = getOutputMediaFile();
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(bytes);
                fos.close();
                Uri picUri = Uri.fromFile(pictureFile);
                updateGallery(picUri);
                test = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

现在我只想获取其中一个预览并在转换为 mat 后保存它。

在花了无数小时让上面看起来正确之后,保存的图片在我的测试中看不到 phone (LG Leon)。我似乎找不到问题所在。我混合 height/width 是因为我在纵向模式下拍照吗?我尝试切换它们,但仍然无法正常工作。问题出在哪里?

我最近提出的问题 中描述了我设法找到的最快方法。您可以在我在下面的问题中写的答案中找到提取图像的方法。问题是你通过 onPreviewFrame() 得到的图像是 NV21。收到这张图片后可能需要转成RGB(看你想达到什么效果,我之前给你的答案里也有)。

看起来效率很低,但对我有用(目前):

//get the camera parameters
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;

//convert the byte[] to Bitmap through YuvImage; 
//make sure the previewFormat is NV21 (I set it so somewhere before)
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 70, out);
Bitmap bmp = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());

//convert Bitmap to Mat; note the bitmap config ARGB_8888 conversion that 
//allows you to use other image processing methods and still save at the end
Mat orig = new Mat();
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp, orig);

//here you do whatever you want with the Mat

//Mat to Bitmap to OutputStream to byte[] to File
Utils.matToBitmap(orig, bmp);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);
byte[] bytes = stream.toByteArray();
File pictureFile = getOutputMediaFile();
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    fos.write(bytes);
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}