gpt4 book ai didi

android - 位图大小超出 VM 预算

转载 作者:行者123 更新时间:2023-11-29 14:00:43 26 4
gpt4 key购买 nike

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

  1. 旋转位图
  2. 为位图添加水印叠加层
  3. 在位图的右下角添加日期

调用的所有 3 个方法都将创建一个返回位图对象,这可能会导致崩溃,因为我试图每 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;

}

最佳答案

先试试这个:变化

// 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 x 50px)。

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

关于android - 位图大小超出 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com