How to convert YUV_420_888 image to bitmap [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • Android Camera2 API YUV_420_888 to JPEG

    3 answers




I am working on AR project where i need to capture the current frame and save it to gallery. I am able to get the image using Frame class in AR core , but the format of image is YUV_420_888. I have already tried lots of solutions to covert this to bitmap but couldn't able to solve it.










share|improve this question













marked as duplicate by Community Jan 8 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    1
















    This question already has an answer here:




    • Android Camera2 API YUV_420_888 to JPEG

      3 answers




    I am working on AR project where i need to capture the current frame and save it to gallery. I am able to get the image using Frame class in AR core , but the format of image is YUV_420_888. I have already tried lots of solutions to covert this to bitmap but couldn't able to solve it.










    share|improve this question













    marked as duplicate by Community Jan 8 at 8:51


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • Android Camera2 API YUV_420_888 to JPEG

        3 answers




      I am working on AR project where i need to capture the current frame and save it to gallery. I am able to get the image using Frame class in AR core , but the format of image is YUV_420_888. I have already tried lots of solutions to covert this to bitmap but couldn't able to solve it.










      share|improve this question















      This question already has an answer here:




      • Android Camera2 API YUV_420_888 to JPEG

        3 answers




      I am working on AR project where i need to capture the current frame and save it to gallery. I am able to get the image using Frame class in AR core , but the format of image is YUV_420_888. I have already tried lots of solutions to covert this to bitmap but couldn't able to solve it.





      This question already has an answer here:




      • Android Camera2 API YUV_420_888 to JPEG

        3 answers








      android android-image android-bitmap arcore sceneform






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 6:04









      Keval ShuklaKeval Shukla

      1209




      1209




      marked as duplicate by Community Jan 8 at 8:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Community Jan 8 at 8:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          This is how I convert to jpeg.



          public Bitmap imageToBitmap(Image image, float rotationDegrees) {

          assert (image.getFormat() == ImageFormat.NV21);

          // NV21 is a plane of 8 bit Y values followed by interleaved Cb Cr
          ByteBuffer ib = ByteBuffer.allocate(image.getHeight() * image.getWidth() * 2);

          ByteBuffer y = image.getPlanes()[0].getBuffer();
          ByteBuffer cr = image.getPlanes()[1].getBuffer();
          ByteBuffer cb = image.getPlanes()[2].getBuffer();
          ib.put(y);
          ib.put(cb);
          ib.put(cr);

          YuvImage yuvImage = new YuvImage(ib.array(),
          ImageFormat.NV21, image.getWidth(), image.getHeight(), null);

          ByteArrayOutputStream out = new ByteArrayOutputStream();
          yuvImage.compressToJpeg(new Rect(0, 0,
          image.getWidth(), image.getHeight()), 50, out);
          byte imageBytes = out.toByteArray();
          Bitmap bm = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
          Bitmap bitmap = bm;

          // On android the camera rotation and the screen rotation
          // are off by 90 degrees, so if you are capturing an image
          // in "portrait" orientation, you'll need to rotate the image.
          if (rotationDegrees != 0) {
          Matrix matrix = new Matrix();
          matrix.postRotate(rotationDegrees);
          Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm,
          bm.getWidth(), bm.getHeight(), true);
          bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
          scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
          }
          return bitmap;
          }





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            This is how I convert to jpeg.



            public Bitmap imageToBitmap(Image image, float rotationDegrees) {

            assert (image.getFormat() == ImageFormat.NV21);

            // NV21 is a plane of 8 bit Y values followed by interleaved Cb Cr
            ByteBuffer ib = ByteBuffer.allocate(image.getHeight() * image.getWidth() * 2);

            ByteBuffer y = image.getPlanes()[0].getBuffer();
            ByteBuffer cr = image.getPlanes()[1].getBuffer();
            ByteBuffer cb = image.getPlanes()[2].getBuffer();
            ib.put(y);
            ib.put(cb);
            ib.put(cr);

            YuvImage yuvImage = new YuvImage(ib.array(),
            ImageFormat.NV21, image.getWidth(), image.getHeight(), null);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            yuvImage.compressToJpeg(new Rect(0, 0,
            image.getWidth(), image.getHeight()), 50, out);
            byte imageBytes = out.toByteArray();
            Bitmap bm = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            Bitmap bitmap = bm;

            // On android the camera rotation and the screen rotation
            // are off by 90 degrees, so if you are capturing an image
            // in "portrait" orientation, you'll need to rotate the image.
            if (rotationDegrees != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotationDegrees);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm,
            bm.getWidth(), bm.getHeight(), true);
            bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
            scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
            }
            return bitmap;
            }





            share|improve this answer




























              1














              This is how I convert to jpeg.



              public Bitmap imageToBitmap(Image image, float rotationDegrees) {

              assert (image.getFormat() == ImageFormat.NV21);

              // NV21 is a plane of 8 bit Y values followed by interleaved Cb Cr
              ByteBuffer ib = ByteBuffer.allocate(image.getHeight() * image.getWidth() * 2);

              ByteBuffer y = image.getPlanes()[0].getBuffer();
              ByteBuffer cr = image.getPlanes()[1].getBuffer();
              ByteBuffer cb = image.getPlanes()[2].getBuffer();
              ib.put(y);
              ib.put(cb);
              ib.put(cr);

              YuvImage yuvImage = new YuvImage(ib.array(),
              ImageFormat.NV21, image.getWidth(), image.getHeight(), null);

              ByteArrayOutputStream out = new ByteArrayOutputStream();
              yuvImage.compressToJpeg(new Rect(0, 0,
              image.getWidth(), image.getHeight()), 50, out);
              byte imageBytes = out.toByteArray();
              Bitmap bm = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
              Bitmap bitmap = bm;

              // On android the camera rotation and the screen rotation
              // are off by 90 degrees, so if you are capturing an image
              // in "portrait" orientation, you'll need to rotate the image.
              if (rotationDegrees != 0) {
              Matrix matrix = new Matrix();
              matrix.postRotate(rotationDegrees);
              Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm,
              bm.getWidth(), bm.getHeight(), true);
              bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
              scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
              }
              return bitmap;
              }





              share|improve this answer


























                1












                1








                1







                This is how I convert to jpeg.



                public Bitmap imageToBitmap(Image image, float rotationDegrees) {

                assert (image.getFormat() == ImageFormat.NV21);

                // NV21 is a plane of 8 bit Y values followed by interleaved Cb Cr
                ByteBuffer ib = ByteBuffer.allocate(image.getHeight() * image.getWidth() * 2);

                ByteBuffer y = image.getPlanes()[0].getBuffer();
                ByteBuffer cr = image.getPlanes()[1].getBuffer();
                ByteBuffer cb = image.getPlanes()[2].getBuffer();
                ib.put(y);
                ib.put(cb);
                ib.put(cr);

                YuvImage yuvImage = new YuvImage(ib.array(),
                ImageFormat.NV21, image.getWidth(), image.getHeight(), null);

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                yuvImage.compressToJpeg(new Rect(0, 0,
                image.getWidth(), image.getHeight()), 50, out);
                byte imageBytes = out.toByteArray();
                Bitmap bm = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                Bitmap bitmap = bm;

                // On android the camera rotation and the screen rotation
                // are off by 90 degrees, so if you are capturing an image
                // in "portrait" orientation, you'll need to rotate the image.
                if (rotationDegrees != 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(rotationDegrees);
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm,
                bm.getWidth(), bm.getHeight(), true);
                bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
                }
                return bitmap;
                }





                share|improve this answer













                This is how I convert to jpeg.



                public Bitmap imageToBitmap(Image image, float rotationDegrees) {

                assert (image.getFormat() == ImageFormat.NV21);

                // NV21 is a plane of 8 bit Y values followed by interleaved Cb Cr
                ByteBuffer ib = ByteBuffer.allocate(image.getHeight() * image.getWidth() * 2);

                ByteBuffer y = image.getPlanes()[0].getBuffer();
                ByteBuffer cr = image.getPlanes()[1].getBuffer();
                ByteBuffer cb = image.getPlanes()[2].getBuffer();
                ib.put(y);
                ib.put(cb);
                ib.put(cr);

                YuvImage yuvImage = new YuvImage(ib.array(),
                ImageFormat.NV21, image.getWidth(), image.getHeight(), null);

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                yuvImage.compressToJpeg(new Rect(0, 0,
                image.getWidth(), image.getHeight()), 50, out);
                byte imageBytes = out.toByteArray();
                Bitmap bm = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                Bitmap bitmap = bm;

                // On android the camera rotation and the screen rotation
                // are off by 90 degrees, so if you are capturing an image
                // in "portrait" orientation, you'll need to rotate the image.
                if (rotationDegrees != 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(rotationDegrees);
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm,
                bm.getWidth(), bm.getHeight(), true);
                bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
                }
                return bitmap;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 18:31









                Clayton WilkinsonClayton Wilkinson

                3,5591921




                3,5591921

















                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith