【问题标题】:byte[] to image using Xamarin.Androidbyte[] 到使用 Xamarin.Android 的图像
【发布时间】:2016-02-08 10:43:47
【问题描述】:

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

背景:我正在编写一个接收picturebytes via UDP 的Andoid-App,将它们编码为位图并在image view 中显示图片。

由于我的函数不起作用,我取消了UDP-Connection 进行测试,并将所有image-bytes 写入一个巨大的变量中。所以他们都是正确的... 该函数返回“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;
}

最不幸的是,最后一个函数效果不佳,但在这里我承认,我对 string imatge = System.IO.Path.Combine (documents, "images", "image.jpg"); 我收到一个错误并将其更改为documentsFolder,因为我猜应该(或可能)是正确的......

提前感谢您的帮助

【问题讨论】:

    标签: android image bitmap xamarin byte


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我做了类似的事情

      在发送方:

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

      【讨论】:

        【解决方案3】:

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

        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-04
          • 1970-01-01
          • 2012-04-06
          • 2012-09-30
          • 1970-01-01
          • 1970-01-01
          • 2014-10-04
          • 1970-01-01
          相关资源
          最近更新 更多