Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android openCV byte[] to mat to byte[]

My goal is to add an overlay on the camera preview that will find book edges. For that, I override the onPreviewFrame where I do the following:

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();
        }
    }
}

For now I simply want to take one of the previews and save it after the conversion to mat.

After spending countless hours getting the above to look right, the saved picture cannot be seen on my testing phone (LG Leon). I can't seem to find the issue. Am I mixing the height/width because I'm taking pictures in portrait mode? I tried switching them and still doesn't work. Where is the problem?