byte[] 到图像使用 Xamarin.Android

byte[] to image using Xamarin.Android

我知道,这是一个老问题,但我在将 byte[] 编码为位图时遇到了问题...

背景:我正在编写一个接收 picturebytes via UDP 的 Andoid-App,将它们编码成位图并以 image view.

格式显示图片

由于我的函数不起作用,我取消了UDP-Connection进行测试,并将所有image-bytes写在一个巨大的变量中。所以他们都是对的... 函数 returns "null"。 我正在使用的功能:

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

我尝试的另一个功能:

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

我在网上找到的一个函数:

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;
}

最不幸的是,最后一个函数也没有用,但在这里我承认,我对 'documents' 中的 'documents' 有点困惑 string imatge = System.IO.Path.Combine (documents, "images", "image.jpg"); 我得到一个错误并将其更改为 documentsFolder 因为我猜,那应该(或可能)是正确的....

提前感谢您的帮助

我做了类似的事情

发送方:

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
                }

接收方:

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);
                        }
                    });

有一个内置方法可以将字节数组解码为位图。当我们谈论大图像时,问题就来了。对于小的,您可以使用:

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

注意。这些位图是不可变的,因此您将无法在这些位图上使用画布。要使它们可变,请转到:BitmapFactory.decodeResource returns a mutable Bitmap in Android 2.2 and an immutable Bitmap in Android 1.6

看来,我发现了错误…… 我将 public Bitmap ByteArrayToImage(byte[] imageData) 存储在另一个 class 中。我不知道为什么,但是当我解码也接收数组的 class 中的 Bytearray 时,一切正常...... 如果有人知道原因,欢迎告诉我,但现在我很高兴 ;-)