3

I had a Binary image. Now, How to when I will click on Button I want to take its.? Please Help me.

This is my code:

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) {}
}

1 Answer 1

3

So with reference to your sample code. You can do that in two ways,

  1. You can either use the OpenCV onPictureTaken method to capture the image and store it in your storage.

    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);
    }
    }
    

    and place this code inside onClick Listener

    mOpenCvCameraView.takePicture(fileName);
    

    but the thing is, the image that is sent as preview to the camera will be saved as default(if its RBG image then RGB image will be saved, if its binary then binary image will be saved)

  2. Or you could create a button on Android and in onClick listener you can use imwrite and save the image, if your camera supports onTouch capture you could also use it in onTouch method. paste this code inside onClick listener or inside onTouch listener (its your wish to choose).

    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);
    
3
  • Thank you very much. I will try it.
    – lucky
    Commented Mar 20, 2017 at 16:43
  • Any luck ? If it solves your problem, then accept the answer and upvote it so that this could help others Commented Mar 21, 2017 at 14:57
  • JavaCamera2View implementation does not seem to have takePicture method. By chance do you have any knowledge on this topic?
    – mcy
    Commented Mar 11, 2021 at 14:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.