Android Camera: How to process byte data in onPictureTaken using openCV












0














I'm trying to process an image that comes from a JpegImageCallback like this



    mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mContentView.getCamera().setPreviewCallback(null);
mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
}
});


The JpegImageCallback looks like this



    class JpegImageCallback implements Camera.PictureCallback {

@Override
public void onPictureTaken(byte data, Camera camera) {
Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);

Camera.Parameters params = camera.getParameters();
int fw = params.getPictureSize().width;
int fh = params.getPictureSize().height;

Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */

/* conversion data to Mat */
Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
mYuvFrameData.put(0, 0, data);
Mat rgbImage = new Mat();
Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
}
}


The conversion from data to org.opencv.core.Mat is copied from org.opencv.android.JavaCameraView. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera) in a Camera.PreviewCallback. But in a Camera.PictureCallbacklike above I get the image wrong.



After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).



Image comes correct in onPreviewFrameimage comes wrong in onPictureTaken



I already changed the image format to be the same in preview and picture (ImageFormat.NV21), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG if I don't change it.



So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken like in onPreviewFrame?



Thanks for any ideas!



Edit:
I dont't want to save the image to disk and I don't want to convert it to a bitmap.










share|improve this question





























    0














    I'm trying to process an image that comes from a JpegImageCallback like this



        mContentView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    mContentView.getCamera().setPreviewCallback(null);
    mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
    }
    });


    The JpegImageCallback looks like this



        class JpegImageCallback implements Camera.PictureCallback {

    @Override
    public void onPictureTaken(byte data, Camera camera) {
    Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);

    Camera.Parameters params = camera.getParameters();
    int fw = params.getPictureSize().width;
    int fh = params.getPictureSize().height;

    Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
    Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */

    /* conversion data to Mat */
    Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
    mYuvFrameData.put(0, 0, data);
    Mat rgbImage = new Mat();
    Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
    Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
    }
    }


    The conversion from data to org.opencv.core.Mat is copied from org.opencv.android.JavaCameraView. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera) in a Camera.PreviewCallback. But in a Camera.PictureCallbacklike above I get the image wrong.



    After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).



    Image comes correct in onPreviewFrameimage comes wrong in onPictureTaken



    I already changed the image format to be the same in preview and picture (ImageFormat.NV21), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG if I don't change it.



    So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken like in onPreviewFrame?



    Thanks for any ideas!



    Edit:
    I dont't want to save the image to disk and I don't want to convert it to a bitmap.










    share|improve this question



























      0












      0








      0







      I'm trying to process an image that comes from a JpegImageCallback like this



          mContentView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      mContentView.getCamera().setPreviewCallback(null);
      mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
      }
      });


      The JpegImageCallback looks like this



          class JpegImageCallback implements Camera.PictureCallback {

      @Override
      public void onPictureTaken(byte data, Camera camera) {
      Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);

      Camera.Parameters params = camera.getParameters();
      int fw = params.getPictureSize().width;
      int fh = params.getPictureSize().height;

      Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
      Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */

      /* conversion data to Mat */
      Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
      mYuvFrameData.put(0, 0, data);
      Mat rgbImage = new Mat();
      Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
      Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
      }
      }


      The conversion from data to org.opencv.core.Mat is copied from org.opencv.android.JavaCameraView. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera) in a Camera.PreviewCallback. But in a Camera.PictureCallbacklike above I get the image wrong.



      After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).



      Image comes correct in onPreviewFrameimage comes wrong in onPictureTaken



      I already changed the image format to be the same in preview and picture (ImageFormat.NV21), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG if I don't change it.



      So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken like in onPreviewFrame?



      Thanks for any ideas!



      Edit:
      I dont't want to save the image to disk and I don't want to convert it to a bitmap.










      share|improve this question















      I'm trying to process an image that comes from a JpegImageCallback like this



          mContentView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      mContentView.getCamera().setPreviewCallback(null);
      mContentView.getCamera().takePicture(null, null, new JpegImageCallback());
      }
      });


      The JpegImageCallback looks like this



          class JpegImageCallback implements Camera.PictureCallback {

      @Override
      public void onPictureTaken(byte data, Camera camera) {
      Log.d(TAG, "onPictureTaken: I am here! data.length=" + data.length);

      Camera.Parameters params = camera.getParameters();
      int fw = params.getPictureSize().width;
      int fh = params.getPictureSize().height;

      Log.d(TAG, "onPictureTaken: PictureFormat: " + params.getPictureFormat()); /* ImageFormat.NV21 */
      Log.d(TAG, "onPictureTaken: PreviewFormat: " + params.getPreviewFormat()); /* ImageFormat.NV21 */

      /* conversion data to Mat */
      Mat mYuvFrameData = new Mat(fh + (fh/2), fw, CvType.CV_8UC1);
      mYuvFrameData.put(0, 0, data);
      Mat rgbImage = new Mat();
      Imgproc.cvtColor(mYuvFrameData, rgbImage, Imgproc.COLOR_YUV2RGBA_NV21, 4);
      Mat gryImage = mYuvFrameData.submat(0, fh, 0, fw);
      }
      }


      The conversion from data to org.opencv.core.Mat is copied from org.opencv.android.JavaCameraView. It works totally fine when the data comes in onPreviewFrame(byte frame, Camera camera) in a Camera.PreviewCallback. But in a Camera.PictureCallbacklike above I get the image wrong.



      After the image is taken I display it in a ResultActivity, see below: left is from preview and correct, right is from picture and wrong (green).



      Image comes correct in onPreviewFrameimage comes wrong in onPictureTaken



      I already changed the image format to be the same in preview and picture (ImageFormat.NV21), but that didn't make a difference. The image format for the picture is ImageFormat.JPEG if I don't change it.



      So the question is: How do I convert data to Mat to get a correct Image in onPictureTaken like in onPreviewFrame?



      Thanks for any ideas!



      Edit:
      I dont't want to save the image to disk and I don't want to convert it to a bitmap.







      android opencv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 14:31

























      asked Nov 23 '18 at 13:29









      Julia Kurde

      246




      246
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This works:



                  Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
          mat.put(0, 0, data);

          Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
          Mat gryImage = new Mat();
          Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);


          Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?



          Imgcodecs.imdecode does all the magic!



          There is no need to change the image format, by the way.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447626%2fandroid-camera-how-to-process-byte-data-in-onpicturetaken-using-opencv%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            This works:



                    Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
            mat.put(0, 0, data);

            Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
            Mat gryImage = new Mat();
            Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);


            Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?



            Imgcodecs.imdecode does all the magic!



            There is no need to change the image format, by the way.






            share|improve this answer


























              0














              This works:



                      Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
              mat.put(0, 0, data);

              Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
              Mat gryImage = new Mat();
              Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);


              Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?



              Imgcodecs.imdecode does all the magic!



              There is no need to change the image format, by the way.






              share|improve this answer
























                0












                0








                0






                This works:



                        Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
                mat.put(0, 0, data);

                Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
                Mat gryImage = new Mat();
                Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);


                Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?



                Imgcodecs.imdecode does all the magic!



                There is no need to change the image format, by the way.






                share|improve this answer












                This works:



                        Mat mat = new Mat(new Size(fw, fh), CvType.CV_8U);
                mat.put(0, 0, data);

                Mat rgbImage = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
                Mat gryImage = new Mat();
                Imgproc.cvtColor(rgbImage, gryImage, Imgproc.COLOR_RGB2GRAY);


                Found it here: How to retrieve a new picture taken by the camera as OpenCV Mat on Android?



                Imgcodecs.imdecode does all the magic!



                There is no need to change the image format, by the way.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 13:29









                Julia Kurde

                246




                246






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447626%2fandroid-camera-how-to-process-byte-data-in-onpicturetaken-using-opencv%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)