Ask Your Question

Android OpenCV capture image

asked 2017-06-10 21:40:11 -0500

lirus7 gravatar image

I have done a lot of research on internet like probably gone through all the answers but cant find what the error is. I am trying to just save a picture if it goes above a certain value which obtained from a C++ function.But Im not able to capture that particulat frame.I am trying to use imwrite() in OnCameraFrame().The logcat shows the image has been written successfully but i cant find the image anywhere on my phone(External Storage) and below is the manifest.xml of the app(permissions) .

@Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { mRgba=inputFrame.rgba(); String ans=cvClass.detect(mRgba.getNativeObjAddr()); Log.i(TAG,ans); // String filename="/home/Downloads/test1.bmp"; if(Double.parseDouble(ans)>2.5) { Log.i(TAG,"try"); String filename = "please.bmp";(tried both jpg and bmp)

    File path = Environment.getExternalStorageDirectory();
    File file = new File(path, filename);
    Boolean bool = null;
    filename = file.toString();
    bool = Imgcodecs.imwrite(filename,mRgba);

    if (bool == true){
        Log.d(TAG, "SUCCESS writing image to external storage");
        Log.d(TAG, filename);
        Log.d(TAG, "Fail writing image to external storage");

return mRgba;



<uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name=""/> <uses-permissionandroid:name="android.permission.write_external_storage"/>

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-12-04 03:50:23 -0500

Akhil Patel gravatar image

I hope following code are useful for you. try this once.

   public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) 

          Mat rgba = inputFrame.rgba();
          Bitmap capturebitmap = Bitmap.createBitmap(rgba.cols(), rgba.rows(), Bitmap.Config.ARGB_8888);
          Utils.matToBitmap(rgba, capturebitmap true);

          //create a file to write bitmap data
          File f = new File(context.getCacheDir(), filename);

         //Convert bitmap to byte array
         Bitmap bitmap = capturebitmap;
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
         byte[] bitmapdata = bos.toByteArray();

         //write the bytes in file
         FileOutputStream fos = new FileOutputStream(f);

         return rgba
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-06-10 21:40:11 -0500

Seen: 413 times

Last updated: Dec 04