繁体   English   中英

Opencv android,捕获图像

[英]Opencv android, capture image

我将尝试简化我的问题。 可以说我只想使用相机查看灰度图像,我这样做是这样的:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

return inputFrame.gray();

 }

这样就可以了,我现在可以在屏幕上看到灰色的图像了。 但是问题是当我尝试捕获此图像(帧)时。 它不是用灰色捕获它,而是用彩色捕获它(它忽略了我在onCameraFrame中所做的所有效果)。 要捕获图像,我使用与教程3-相机控制相同的方法(此处是相关代码):

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中所做的一切(使图像变灰,检测到圆圈...),但我没有得到任何这些效果,而只是得到了正常图像。

我怎样才能解决这个问题 ?

这是因为您正在onCameraFrame()中修改Mat并将图像数据写入onPictureTaken()中的文件。

onCameraFrame中的Mat永远不会写入文件。

onPictureTaken()中的byte []包含相机在按下按钮或触摸屏幕时(无论触发takePicture()时)所拍摄的照片。

如果要写入具有某些效果的图片,则必须将byte []转换为Mat,然后在此Mat中进行效果,然后使用imwrite()将其写入文件,或将修改后的Mat转换回byte [],然后如代码所示,使用FileOutputStream将其写入文件。

imwrite()将比FileOutputStream方法慢

这是超级骗子,但我在搜索时看到了很多次。 我通过使用解决了我的问题

Imgcodecs.imwrite(fileName,mRgba);

代替

javaCameraView.takePicture(fileName);

在我的onTouch方法中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM