locked
How to get an actual image (i.e. as a file) from an instance of Android.Media.Image captured as JPEG RRS feed

  • Question

  • User368910 posted

    I am continuously getting images from the device camera that need to be send to the main computer via UDP (so the end result is being turned into byte array). However, I do not understed how can I get something from Image that WPF application on the other end would understand. How can I get an actual image from an instance of Image class that I can send over UDP and WPF application will be able to decode?

    Wednesday, October 2, 2019 11:10 AM

All replies

  • User379860 posted

    Do you want to convert the Android.Media.Image to the Byte array? If so, you can try to convert it by following code.

      data = NV21toJPEG(
                   YUV_420_888toNV21(image),
                        image.Width, image.Height);
    
         private static byte[] YUV_420_888toNV21(Image image)
        {
    
            byte[] nv21;
            ByteBuffer yBuffer = image.GetPlanes()[0].Buffer ;
            ByteBuffer uBuffer = image.GetPlanes()[1].Buffer;
            ByteBuffer vBuffer = image.GetPlanes()[2].Buffer;
    
            int ySize = yBuffer.Remaining() ;
            int uSize = uBuffer.Remaining();
            int vSize = vBuffer.Remaining();
    
            nv21 = new byte[ySize + uSize + vSize];
    
            //U and V are swapped
            yBuffer.Get(nv21, 0, ySize);
            vBuffer.Get(nv21, ySize, vSize);
            uBuffer.Get(nv21, ySize + vSize, uSize);
    
            return nv21;
        }
    
    
        private static byte[] NV21toJPEG(byte[] nv21, int width, int height)
        {
    
    
         System.IO.MemoryStream outstran = new System.IO.MemoryStream();
            YuvImage yuv = new YuvImage(nv21, ImageFormat.Nv21, width, height, null);
            yuv.CompressToJpeg(new Rect(0, 0, width, height), 100, outstran);
    
            return outstran.ToArray();
        }
    
    Wednesday, October 2, 2019 12:47 PM
  • User368910 posted

    @LeonLu said: Do you want to convert the Android.Media.Image to the Byte array? If so, you can try to convert it by following code.

      data = NV21toJPEG(
                   YUV_420_888toNV21(image),
                        image.Width, image.Height);
    
         private static byte[] YUV_420_888toNV21(Image image)
        {
    
            byte[] nv21;
            ByteBuffer yBuffer = image.GetPlanes()[0].Buffer ;
            ByteBuffer uBuffer = image.GetPlanes()[1].Buffer;
            ByteBuffer vBuffer = image.GetPlanes()[2].Buffer;
    
            int ySize = yBuffer.Remaining() ;
            int uSize = uBuffer.Remaining();
            int vSize = vBuffer.Remaining();
    
            nv21 = new byte[ySize + uSize + vSize];
    
            //U and V are swapped
            yBuffer.Get(nv21, 0, ySize);
            vBuffer.Get(nv21, ySize, vSize);
            uBuffer.Get(nv21, ySize + vSize, uSize);
    
            return nv21;
        }
    
    
        private static byte[] NV21toJPEG(byte[] nv21, int width, int height)
        {
    
    
         System.IO.MemoryStream outstran = new System.IO.MemoryStream();
            YuvImage yuv = new YuvImage(nv21, ImageFormat.Nv21, width, height, null);
            yuv.CompressToJpeg(new Rect(0, 0, width, height), 100, outstran);
    
            return outstran.ToArray();
        }
    

    No, converting to byte[] is not a goal, I just mentioned that because a lot of times conversion involves byte arrays and streams, so I thought it could be useful to whoever is giving an aswer and save them some lines of code.

    I have an Android.Media.Image - a result of JPEG capture. I need to turn it into something a WPF app would understand. I am sending it as byte[], as UDP API in .NET requires.

    Wednesday, October 2, 2019 1:04 PM
  • User379860 posted

    so I thought it could be useful to whoever is giving an aswer and save them some lines of code. Do you want to fewer code to implement it? Normally, we send the Byte[] to the server, then convert it to the Image.

    Wednesday, October 2, 2019 1:24 PM
  • User368910 posted

    @LeonLu said: so I thought it could be useful to whoever is giving an aswer and save them some lines of code. Do you want to fewer code to implement it? Normally, we send the Byte[] to the server, then convert it to the Image.

    Am I not being specific enough or my english is that bad?

    I have an Android.Media.Image, its format is JPEG. I want to turn it into something usable by WPF's Image control and send it via UDP. The question is how to convert it into something usable by WPF's Image control.

    Wednesday, October 2, 2019 1:30 PM