Opencv android,捕获图像 [英] Opencv android, capture image
问题描述
我将尝试简化我的问题.可以说我只是想用相机看到灰度图像,我这样做是这样的:
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();
}
这很好用,现在我可以在屏幕上看到灰色的图像了.但是问题是当我尝试捕获此图像(帧)时.它不是用灰色捕获它,而是用彩色捕获它(它忽略了我在onCameraFrame中所做的所有效果).要捕获图像,我使用与教程3-相机控制相同的方法(此处是相关代码):
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);
}
}
}
因此,回顾一下,问题是当我捕获图像时我在onCameraFrame上所做的一切(使图像变灰,检测圆圈...),但我没有得到任何这些效果,我只是得到了正常图像.
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.
我该如何解决?
推荐答案
这是超级骗子,但我在搜索时看到了很多次.我通过使用
This is super duper old but I saw it so many times while searching. I solved my issue by using
Imgcodecs.imwrite(fileName,mRgba);
代替
javaCameraView.takePicture(fileName);
在我的onTouch方法中.
in my onTouch method.
这篇关于Opencv android,捕获图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!