3

我有一个二进制图像。现在,当我点击我想要的按钮时,我该怎么做。?请帮我。

这是我的代码:

public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2, Camera.PictureCallback {

    private static final String TAG = "MainActivity";
    private JavaCameraView mOpenCvCameraView;
    private static final int REQUEST_CODE = 123;
    private Button mButton;
    private Mat mRgba;
    private Mat mByte;
    private Mat imGgray, imgCandy;
    CameraBridgeViewBase.CvCameraViewFrame inputFrame;

    BaseLoaderCallback mBaseLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            super.onManagerConnected(status);
            switch (status) {
                case BaseLoaderCallback.SUCCESS:
                    {
                        mOpenCvCameraView.enableView();
                        break;
                    }
                default:
                    {
                        super.onManagerConnected(status);
                        break;
                    }
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.detetionbutton);
        mOpenCvCameraView = (JavaCameraView) findViewById(R.id.textdetetion_view);
        mOpenCvCameraView.setCvCameraViewListener(this);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
        mButton.setOnClickListener(new View.OnClickListener() { // take a picture
            @Override
            public void onClick(View v) {

            }
        });

    }
    @Override
    public void onPause() {
        super.onPause();
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        if (OpenCVLoader.initDebug() == true) {
            Log.i(TAG, "opencv loaded");
            mBaseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        } else {
            Log.i(TAG, "opencn not loade");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mBaseLoaderCallback);
        }
    }
    @Override
    public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat(height, width, CvType.CV_8UC4);
        mByte = new Mat(height, width, CvType.CV_8UC4);
        imGgray = new Mat(height, width, CvType.CV_8UC4);
        imgCandy = new Mat(height, width, CvType.CV_8UC1);
    }
    @Override
    public void onCameraViewStopped() {
        mRgba.release();
    }
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        Imgproc.cvtColor(mRgba, imGgray, Imgproc.COLOR_RGB2GRAY);
        Imgproc.adaptiveThreshold(imGgray, mByte, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 35, 5);
        return mByte; // this is m Binary image
    }
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {}
}
4

1 回答 1

3

所以参考你的示例代码。你可以通过两种方式做到这一点,

  1. 您可以使用 OpenCV onPictureTaken方法来捕获图像并将其存储在您的存储中。

    public void onPictureTaken(byte[] data, Camera camera) {
    
    //Log.i(TAG, "Saving a bitmap to file");
    // The camera preview was automatically stopped. Start it again.
    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);
    }
    }
    

    并将此代码放在onClick侦听器中

    mOpenCvCameraView.takePicture(fileName);
    

    但问题是,作为预览发送到相机的图像将被保存为默认值(如果它的 RBG 图像将被保存,则 RGB 图像将被保存,如果它的二进制图像将被保存)

  2. 或者您可以在 Android 上创建一个按钮,在onClick侦听器中您可以使用imwrite并保存图像,如果您的相机支持 onTouch 捕获,您也可以在onTouch方法中使用它。将此代码粘贴到onClick侦听器或onTouch 侦听器中(您可以选择)。

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    String currentDateandTime = sdf.format(new Date());
    String fileName = Environment.getExternalStorageDirectory().getPath() +
            "/sample_picture_" + currentDateandTime + ".jpg";
    Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
    String filename = "/storage/emulated/0/DCIM/Camera/samplepass.jpg";
    Highgui.imwrite(filename,mByte);
    
于 2017-03-20T14:52:56.660 回答