Android合并两张图片

发布于 2024-11-27 16:57:31 字数 1374 浏览 1 评论 0原文

我想合并两张图像,然后将它们保存在 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());

现在的问题是它不保存图像。你知道为什么吗?

I want to merge two images and then save them on the Android SDCard.One is from the camera and one from the resources folder. The problem is that i get this error: Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor. Thanks.

        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();
        }

Managed to fix it by simply doing this change:

        int w = bottomImage.getWidth();
        int h = bottomImage.getHeight(); 
        Bitmap new_image = Bitmap.createBitmap(w, h ,bottomImage.getConfig());

The problem now is that it doesn't saves the image. Do you know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音 2024-12-04 16:57:31

会对您有帮助=)


编辑:(嵌入来自链接)

返回可变位图的唯一静态“构造函数”是:

(类:位图) public static Bitmap createBitmap(int width, int
高度,布尔值 h​​asAlpha)
返回:具有指定宽度和高度的可变位图。

  • 所以你可以使用 getPixels/setPixels 或像这样:

    位图 bitmapResult = bm.createBitmap(widthOfOld, heightOfOld, hasAlpha);
    画布 c = new Canvas();
    c.setDevice(bitmapResult); // drawXY 将在该位图上生成
    c.drawBitmap(位图旧,左,上,油漆);
    
  • 如何从 Bitmap 获取可绘制对象:通过使用扩展了可绘制对象的 BitmapDrawable 子类,如下所示:

    位图 myBitmap = BitmapFactory.decode(path);
    Drawable bd = new BitmapDrawable(myBitmap);
    

This will help you =)


Edit: (embed answer from link)

the only static "constructor" for Bitmap returning a mutable one is:

(Class: Bitmap) public static Bitmap createBitmap(int width, int
height, boolean hasAlpha)
Returns: a mutable bitmap with the specified width and height.

  • So you could work with getPixels/setPixels or like this:

    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);
    
  • how to get the drawable from Bitmap: by using the BitmapDrawable-Subclass which extends Drawable, like this:

    Bitmap myBitmap = BitmapFactory.decode(path);
    Drawable bd = new BitmapDrawable(myBitmap);
    
生来就爱笑 2024-12-04 16:57:31

您正在检索的位图是不可变的,这意味着它不能修改的。尽管它没有在 Canvas 页面上指定构造函数需要可变位图,确实如此。

要创建可变位图,您可以使用此方法

The bitmap you are retrieving is immutable, meaning it can't be modified. Although it doesn't specify on the Canvas page that the constructor needs a mutable bitmap, it does.

To create a mutable bitmap, you can use this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文