Android – Convertento byte[] do onPreviewFrame em um JPEG ( Android byte[] to image in Camera.onPreviewFrame )

android-camera-surfaceview

import android.graphics.YuvImage;

[...]

private volatile byte[] imageYuvData;

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    if(processing)return;
    imageYuvData = data;
}

[...]

public void savePreviewFrame(File file) throws Throwable {
	processing=true;
	try{
        Camera.Parameters parameters = camera.getParameters();
        Size size = parameters.getPreviewSize();
        YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
                size.width, size.height, null);
        FileOutputStream filecon = new FileOutputStream(file);
        image.compressToJpeg(
        		new Rect(0, 0, image.getWidth(), 
        				image.getHeight()), 90,
        				filecon);
	}finally{
		processing=false;
	}
}

[...]