我想合并两张图像,然后将它们保存在 Android SDCard 上。一张来自相机,一张来自资源文件夹。问题是我收到此错误: Caused by: java.lang.IllegalStateException: 不可变位图传递给 Canvas 构造函数。谢谢。
Bitmap bottomImage = BitmapFactory.decodeResource(getResources(),R.drawable.blink);
Bitmap topImage = (Bitmap) data.getExtras().get("data");
// As described by Steve Pomeroy in a previous comment,
// use the canvas to combine them.
// Start with the first in the constructor..
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);
// bottomImage is now a composite of the two.
// To write the file out to the SDCard:
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
bottomImage.compress(CompressFormat.PNG, 50, os);
//Bitmap image.compress(CompressFormat.PNG, 50, os);
} catch(IOException e) {
Log.v("error saving","error saving");
e.printStackTrace();
}
只需进行以下更改即可解决此问题:
int w = bottomImage.getWidth();
int h = bottomImage.getHeight();
Bitmap new_image = Bitmap.createBitmap(w, h ,bottomImage.getConfig());
现在的问题是它不保存图像。你知道为什么吗?
This会帮助你=)
Edit:(嵌入链接中的答案)
返回可变位图的唯一静态“构造函数”是:
(类:位图) public static Bitmap createBitmap(int width, int
高度,布尔值 hasAlpha)
返回: 具有指定宽度和高度的可变位图。
-
所以你可以使用 getPixels/setPixels 或像这样:
Bitmap bitmapResult = bm.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c = new Canvas();
c.setDevice(bitmapResult); // drawXY will result on that Bitmap
c.drawBitmap(bitmapOld, left, top, paint);
-
如何从 Bitmap 获取可绘制对象:通过使用扩展了可绘制对象的 BitmapDrawable 子类,如下所示:
Bitmap myBitmap = BitmapFactory.decode(path);
Drawable bd = new BitmapDrawable(myBitmap);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)