处理和处理多个大位图时的内存问题

7

我需要让用户通过设备相机一个一个地拍摄10张照片,然后对每一张进行灰度处理,最后将它们上传到服务器。

现在,在低端安卓设备(内存小于1GB)中,我遇到了一个问题,由于内存/堆问题,当从相机应用程序返回到应用程序时,页面会再次加载,所有编辑文本数据都保持设置,但是图像视图中的缩略图消失。

步骤:

  1. 拍摄多达10张图片
  2. 每个图片进行灰度处理
  3. 检查方向并保存正确的方向
  4. 显示灰度图像的小缩略图
  5. 第2、3和4步在AsyncTask中完成
  6. 提交时将所有10张图片 (从SD卡中保存的大位图) 与其他数据一起上传到服务器

我参考了以下Android开发者教程:

有关图像压缩和缩放:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

有关图像灰度处理的内容:

bmpGrayscale = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);

针对方向处理:

Uri uri = Uri.parse(picPath);
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(uri.getPath());
    } catch (IOException e) {
        e.printStackTrace();
    }

    int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

    rotationInDegrees = exifToDegrees(rotation);

    private static int exifToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }

    public static Bitmap rotate(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();

            m.setRotate(degrees, (float) b.getWidth() / 2,
                    (float) b.getHeight() / 2);
            // m.postRotate(degrees);
            try {
                System.gc();
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                throw ex;
            }
        }
        return b;
    }

请问有没有人能指导我如何压缩或处理内存,使我能够执行以上步骤而不会发生内存/堆问题。

缓存图像可以帮助吗?我尝试过了,但我的实现方式并没有起到帮助作用。

提前感谢您的帮助。

2个回答

0

我看到你在执行bimap.recycle - 很好。 我发现我必须将每个相机图像保存到磁盘上,然后从服务或同步任务中按顺序处理每个图像。位图非常占用内存,根据手机上的内存大小,一次只能处理几个。在保存到磁盘时,调整jpg压缩比,这样在读回时就会消耗较少的内存。


0
这是我的代码,不确定它的确切工作方式,但它可以在从相机或图库获取位图后节省内存。我可以用这个类处理很多图片。创建一个包含此代码的类,然后在您的活动中调用它。
导入java.io.File;
导入java.io.FileInputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class DecodeImage
{

//decodes image and scales it to reduce memory consumption
public static Bitmap decodeFile(File bitmapFile, int requiredWidth, int requiredHeight, boolean quickAndDirty)
{
    try
    {
        //Decode image size
        BitmapFactory.Options bitmapSizeOptions = new BitmapFactory.Options();
        bitmapSizeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(bitmapFile), null, bitmapSizeOptions);

        // load image using inSampleSize adapted to required image size
        BitmapFactory.Options bitmapDecodeOptions = new BitmapFactory.Options();
        bitmapDecodeOptions.inTempStorage = new byte[16 * 1024];
        bitmapDecodeOptions.inSampleSize = computeInSampleSize(bitmapSizeOptions, requiredWidth, requiredHeight, false);
        bitmapDecodeOptions.inPurgeable = true;
        bitmapDecodeOptions.inDither = !quickAndDirty;
        bitmapDecodeOptions.inPreferredConfig = quickAndDirty ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888;

        Bitmap decodedBitmap = BitmapFactory.decodeStream(new FileInputStream(bitmapFile), null, bitmapDecodeOptions);

        // scale bitmap to mathc required size (and keep aspect ratio)

        float srcWidth = (float) bitmapDecodeOptions.outWidth;
        float srcHeight = (float) bitmapDecodeOptions.outHeight;

        float dstWidth = (float) requiredWidth;
        float dstHeight = (float) requiredHeight;

        float srcAspectRatio = srcWidth / srcHeight;
        float dstAspectRatio = dstWidth / dstHeight;

        // recycleDecodedBitmap is used to know if we must recycle intermediary 'decodedBitmap'
        // (DO NOT recycle it right away: wait for end of bitmap manipulation process to avoid
        // java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@416ee7d8
        // I do not excatly understand why, but this way it's OK

        boolean recycleDecodedBitmap = false;

        Bitmap scaledBitmap = decodedBitmap;
        if (srcAspectRatio < dstAspectRatio)
        {
            scaledBitmap = getScaledBitmap(decodedBitmap, (int) dstWidth, (int) (srcHeight * (dstWidth / srcWidth)));
            // will recycle recycleDecodedBitmap
            recycleDecodedBitmap = true;
        }
        else if (srcAspectRatio > dstAspectRatio)
        {
            scaledBitmap = getScaledBitmap(decodedBitmap, (int) (srcWidth * (dstHeight / srcHeight)), (int) dstHeight);
            recycleDecodedBitmap = true;
        }

        // crop image to match required image size

        int scaledBitmapWidth = scaledBitmap.getWidth();
        int scaledBitmapHeight = scaledBitmap.getHeight();

        Bitmap croppedBitmap = scaledBitmap;

      /**      if (scaledBitmapWidth > requiredWidth)
        {
            int xOffset = (scaledBitmapWidth - requiredWidth) / 2;
            croppedBitmap = Bitmap.createBitmap(scaledBitmap, xOffset, 0, requiredWidth, requiredHeight);
            scaledBitmap.recycle();
        }
        else if (scaledBitmapHeight > requiredHeight)
        {
            int yOffset = (scaledBitmapHeight - requiredHeight) / 2;
            croppedBitmap = Bitmap.createBitmap(scaledBitmap, 0, yOffset, requiredWidth, requiredHeight);
            scaledBitmap.recycle();
        }
**/
            if (recycleDecodedBitmap)
        {
            decodedBitmap.recycle();
        }
        decodedBitmap = null;

        scaledBitmap = null;
        return croppedBitmap;
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    return null;
}

/**
 * compute powerOf2 or exact scale to be used as {@link BitmapFactory.Options#inSampleSize} value (for subSampling)
 * 
 * @param requiredWidth
 * @param requiredHeight
 * @param powerOf2
 *            weither we want a power of 2 sclae or not
 * @return
 */
public static int computeInSampleSize(BitmapFactory.Options options, int dstWidth, int dstHeight, boolean powerOf2)
{
    int inSampleSize = 1;

    // Raw height and width of image
    final int srcHeight = options.outHeight;
    final int srcWidth = options.outWidth;

    if (powerOf2)
    {
        //Find the correct scale value. It should be the power of 2.

        int tmpWidth = srcWidth, tmpHeight = srcHeight;
        while (true)
        {
            if (tmpWidth / 2 < dstWidth || tmpHeight / 2 < dstHeight)
                break;
            tmpWidth /= 2;
            tmpHeight /= 2;
            inSampleSize *= 2;
        }
    }
    else
    {
        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) srcHeight / (float) dstHeight);
        final int widthRatio = Math.round((float) srcWidth / (float) dstWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

public static Bitmap drawableToBitmap(Drawable drawable)
{
    if (drawable instanceof BitmapDrawable)
    {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

public static Bitmap getScaledBitmap(Bitmap bitmap, int newWidth, int newHeight)
{
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // RECREATE THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

}

调用它使用

//imgpath is the path to the image which you can save in shared preferences
   Bitmap bmp=DecodeImage.decodeFile(imgpath, 800, 1000, true);

希望这能有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接