问题
I will try to simplify my problem. Lets say i just want to see grayscale images using camera, i do it this way:
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
return inputFrame.gray();
}
And this works just fine, i can now dinamically see grey image on my screen. But problem is when i try to capture this image (frame). Instead of capturing it in grey, it captures it in color (it ignores all the effects i do in onCameraFrame). To capture image i use the same way done in Tutorial 3 - Camera Control (Here is relevant code):
public class MainActivity extends Activity implements CvCameraViewListener2, View.OnTouchListener {
private CameraLab mOpenCvCameraView;
protected void onCreate(Bundle savedInstanceState) {
mOpenCvCameraView = (CameraLab) findViewById(R.id.openCVCamera);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String currentDateandTime = sdf.format(new Date());
String fileName = "Image_"+currentDateandTime + ".jpg";
mOpenCvCameraView.takePicture(fileName);
return false;
}
}
public class CameraLab extends JavaCameraView implements PictureCallback {
private String mPictureFileName;
public void takePicture(final String fileName) {
Log.i(TAG, "Taking picture");
this.mPictureFileName = fileName;
mCamera.setPreviewCallback(null);
// PictureCallback is implemented by the current class
mCamera.takePicture(null, null, this);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Saving a bitmap to file");
mCamera.startPreview();
mCamera.setPreviewCallback(this);
// Write the image in a file (in jpeg format)
try {
FileOutputStream fos = new FileOutputStream(mPictureFileName);
fos.write(data);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
}
So, to recap, problem is whatever i do in onCameraFrame (make image grey, detect circles...) when i capture image i dont get any of those effects, i just get a normal image.
How can i fix this ?
回答1:
It is because you are modifying Mat in onCameraFrame() and writing the image data to file in onPictureTaken().
The Mat in onCameraFrame is never written to the file.
The byte[] in onPictureTaken() contains the picture taken by the camera when you push a button or touch on the screen whatever is triggering takePicture().
If you want to the write picture with some effects then you will have to convert byte[] to Mat and then make effects in this Mat and then write it to file using imwrite() or convert the modified Mat back to byte[] and then write it to file using FileOutputStream as shown in your code.
imwrite() will be slower than the FileOutputStream approach
回答2:
This is super duper old but I saw it so many times while searching. I solved my issue by using
Imgcodecs.imwrite(fileName,mRgba);
instead of
javaCameraView.takePicture(fileName);
in my onTouch method.
来源:https://stackoverflow.com/questions/30103036/opencv-android-capture-image