0

我的问题是如何在将字节数组解码为位图时处理内存不足错误,以便我可以对其进行旋转。我的代码如下,在你说它重复之前,我已经尝试使用 BitmapFactory.Options 并将样本大小设置为 2。但是质量损失太糟糕了,无法接受。此外,它似乎只发生在一台设备上,所以它可能是一次性的,但我倾向于相信如果它影响一个设备,以后还会有 25 个类似的设备。这也发生在拍摄的第一张照片上,这是该活动对位图所做的唯一工作。当我在 Monodroid 工作时,Java 的答案也很受欢迎,因为我通常可以很容易地将它们翻译成 C#。

public void GotImage(byte[] image)
    {
        try
        {
            Android.Graphics.Bitmap thePicture = Android.Graphics.BitmapFactory.DecodeByteArray(image, 0, image.Length);
            Array.Clear(image, 0, image.Length);
            image = null;
            GC.Collect();
            Android.Graphics.Matrix m = new Android.Graphics.Matrix();
            m.PostRotate(90);
            Android.Graphics.Bitmap rotatedPicture = Android.Graphics.Bitmap.CreateBitmap(thePicture, 0, 0, thePicture.Width, thePicture.Height, m, true);
            thePicture.Dispose();
            thePicture = null;
            GC.Collect();

            using (MemoryStream ms = new MemoryStream())
            {
                rotatedPicture.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, ms);
                image = ms.ToArray();
            }
            rotatedPicture.Dispose();
            rotatedPicture = null;
            GC.Collect();

            listOfImages.Add(image);
            storeButton.Text = "  Store " + listOfImages.Count + " Pages  ";
            storeButton.Enabled = true;
            takePicButton.Enabled = true;
            gotImage = false;
            cameraPreviewArea.camera.StartPreview();
        }
        catch (Exception ex)
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.SetTitle("Error Taking Picture");
            alertDialog.SetMessage(ex.ToString());
            alertDialog.SetPositiveButton("OK", delegate { });
            alertDialog.Show();
        }
    }
4

2 回答 2

0

是什么rotatedPicture.Dispose()?这是否只是将引用设置为空?摆脱位图内存的最好和最快的方法是通过recycle()方法。

于 2012-11-14T15:14:39.470 回答
0

经过一整天的学习,我发现了一个修复/解决方法。这涉及在拍摄照片之前设置相机拍摄的照片的分辨率,而不是在事后尝试对其进行缩放。我还在设置中设置了选项,让用户尝试不同的分辨率,直到他们得到最适合他们的分辨率。

Camera.Parameters parameters = camera.GetParameters();
parameters.SetPictureSize(parameters.SupportedPictureSizes[parameters.SupportedPictureSizes.Count - 1].Width,
                    parameters.SupportedPictureSizes[parameters.SupportedPictureSizes.Count - 1].Height);
camera.SetParameters(parameters);
camera.StartPreview();
于 2012-11-16T23:24:03.233 回答