位图大小超过VM预算

时间:2012-03-07 03:55:04

标签: android

我已经阅读了关于这个问题的那些帖子,但我的情况有所不同,因为我没有显示位图,只是从原始数据后处理图像。 首先,我调用ImageProcessing.rgbToBitmap(data,width, height);,它将返回一个Bitmap对象。然后我分别执行这三个功能。

  1. 旋转位图
  2. 向位图添加水印叠加
  3. 在位图右下角添加日期
  4. 调用的所有3个方法都会创建一个返回一个Bitmap对象,这可能会导致崩溃,因为我试图每1000毫秒保存一个图像!有时保存的图像可能因内存错误而失真。

    我在下面发布我的代码,非常感谢任何建议。我不想在拍摄的图像上妥协。 (需要保留决议)

    public static Bitmap addWatermark(Resources res, Bitmap source) {
            int w, h;
            Canvas c;
            Paint paint;
            Bitmap bmp, watermark;
    
            Matrix matrix;
            float scale;
            RectF r;
    
            w = source.getWidth();
            h = source.getHeight();
    
            // Create the new bitmap
            bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
    
            paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG
                    | Paint.FILTER_BITMAP_FLAG);
    
            // Copy the original bitmap into the new one
            c = new Canvas(bmp);
            c.drawBitmap(source, 0, 0, paint);
    
            // Load the watermark
            watermark = BitmapFactory.decodeResource(res, R.drawable.watermark);
            // Scale the watermark to be approximately 10% of the source image
            // height
            scale = (float) (((float) h * 0.80) / (float) watermark.getHeight());
    
            // Create the matrix
            matrix = new Matrix();
            matrix.postScale(scale, scale);
            // Determine the post-scaled size of the watermark
            r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
            matrix.mapRect(r);
            // Center watermark
            matrix.postTranslate((w - r.width()) / 2, (h - r.height()) / 2);
    
            // Draw the watermark
            c.drawBitmap(watermark, matrix, paint);
            // Free up the bitmap memory
            watermark.recycle();
    
            return bmp;
        }
    
    public static Bitmap addDate(Bitmap src, String date) {
            int w = src.getWidth();
            int h = src.getHeight();
            //Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
            Bitmap result = src;
            Canvas canvas = new Canvas(result);
            canvas.drawBitmap(src, 0, 0, null);
    
            Paint paint = new Paint(); 
            paint.setColor(Color.rgb(255, 185, 15)); 
            paint.setTextSize(20);
            paint.setAntiAlias(true);
            canvas.drawText(date, w - 200, h - 20, paint);
    
            return result;
        }
    public static Bitmap rotate(Bitmap src, int rotation) {
    
            int width = src.getWidth();
            int height = src.getHeight();
    
            // create a matrix for the manipulation
            Matrix matrix = new Matrix();
            // rotate the Bitmap
            matrix.postRotate(rotation);
    
            // recreate the new Bitmap, swap width and height and apply
            // transform
            Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height,
                    matrix, true);
            return rotatedBitmap;
    
        }
    

1 个答案:

答案 0 :(得分:0)

首先尝试: 变化

// Copy the original bitmap into the new one
c = new Canvas(bmp);
c.drawBitmap(source, 0, 0, paint);

// Copy the original bitmap into the new one
c = new Canvas(bmp);
bmp.recycle(); //here or below
c.drawBitmap(source, 0, 0, paint);
//below bmp.recycle();

在这里:

canvas.drawText(date, w - 200, h - 20, paint);

return result;

canvas.drawText(date, w - 200, h - 20, paint);
result.recycle();
return result;

在这里

Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height,
                matrix, true);
return rotatedBitmap;

Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height,
                matrix, true);
return rotatedBitmap;
rotatedBitmap.recycle();

添加所有这些(recycle();)也许这已经足够了,并且尝试使用较小位图的代码,看看它是否仍然崩溃(如50px乘50px)。

不,他们无法增加手机的虚拟机。