MWeb February 2016

byte[] to image using Xamarin.Android

I know, this is an old question, but I've got problems with encoding a byte[] into a bitmap...

Background: I'm writing an Andoid-App which receives picturebytes via UDP, encodes them into a bitmap and displays the picture in an image view.

Since my functions didn't work, I cancelled the UDP-Connection for testing and wrote all the image-bytes in a huge variable. So they're all correct... The function returns "null". The function I'm using:

    public Bitmap ByteArrayToImage(byte[] imageData)
    {
        var bmpOutput = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
        return bmpOutput;
    }

another function I tried out:

    public Bitmap ByteArrayToImage2(byte[] imageData)
    {
        Bitmap bmpReturn;
        bmpReturn = (Android.Graphics.Bitmap) Android.Graphics.Bitmap.FromArray<byte>(imageData);
        return bmpReturn;
    }

A function I found in the internet:

public static Bitmap bytesToUIImage (byte[] bytes)
{
    if (bytes == null)
        return null;

    Bitmap bitmap;


    var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);

    //Create a folder for the images if not exists
    System.IO.Directory.CreateDirectory(System.IO.Path.Combine (documentsFolder, "images"));

    string imatge = System.IO.Path.Combine (documents, "images", "image.jpg");


    System.IO.File.WriteAllBytes(imatge, bytes.Concat(new Byte[]{(byte)0xD9}).ToArray());

    bitmap = BitmapFactory.DecodeFile(imatge);

    return bitmap;
}

Most unfortunately, the last function didn't work as well, but here I have do admit, that I was a bit confused about the 'documents' in string imatge = System.IO.Path.Combine (documents, "images", "image.jpg"); I got an error and changed it into documentsFolder since i guess, that

Answers


Nick Isaacs February 2016

I did something similar

On sender side:

Camera.Parameters parameters = camera.getParameters();

                if (parameters.getPreviewFormat() == ImageFormat.NV21) {
                    Rect rect = new Rect(0, 0, parameters.getPreviewSize().width, parameters.getPreviewSize().height);
                    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, parameters.getPreviewSize().width, parameters.getPreviewSize().height, null);

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    yuvimage.compressToJpeg(rect, 75, os);
                    byte[] videoFrame = os.toByteArray();
                    //send the video frame to reciever
                }

On receiving side:

DataInputStream dIn = new DataInputStream(socket.getInputStream());
                int length = 0;
                length = dIn.readInt();
                if (length > 0) {
                    byte[] message = new byte[length];
                    dIn.readFully(message, 0, message.length);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 4;
                    final Bitmap bitmap = BitmapFactory.decodeByteArray(message, 0, message.length, options);
                    ReceiverActivity.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            imgPreview.setImageBitmap(bitmap);
                        }
                    });


Bogdan Constantin February 2016

There is a built in method to decode a byte array into a bitmap. The problem comes when we are talking of big images. With small ones you can use:

Bitmap bmp = BitmapFactory.DecodeByteArray (data, 0, data.length);

Be aware. Those bitmaps are not mutable, so you will not be able to use canvases on those. To make them mutable go to: BitmapFactory.decodeResource returns a mutable Bitmap in Android 2.2 and an immutable Bitmap in Android 1.6


MWeb February 2016

it seems, I found the error... I stored the public Bitmap ByteArrayToImage(byte[] imageData) in another class. I don't know why, but when I decode the Bytearray in the class that also receives the array, all works fine... If someone knows the reason, feel welcome to let me know, but for now I'm happy ;-)

Post Status

Asked in February 2016
Viewed 2,936 times
Voted 5
Answered 3 times

Search




Leave an answer