如何更改已保存透明位图的背景颜色

发布于 2024-10-09 18:02:54 字数 120 浏览 3 评论 0原文

我正在将 png 图像打开为位图,对其进行一些修改,然后将其作为 jpg 保存到磁盘。如果 png 有一些透明区域,它们将保存为黑色。有没有办法更改此默认行为,以便使用不同的颜色背景(例如白色)保存图像?

谢谢

I am opening a png image into a Bitmap, making some modifications to it and then saving it to disk as a jpg. In the case where the png has some transparent areas, they are saved as black. Is there a way to change this default behavior so the image is saved with a different color background such as white?

Thanks

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

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

发布评论

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

评论(3

黯然#的苍凉 2024-10-16 18:02:54

您可以将其绘制到新的位图,例如

   Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(), 
    image.getHeight(), image.getConfig());
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(image, 0F, 0F, null);

然后保存新的位图

You could draw it to a new bitmap, e.g.

   Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(), 
    image.getHeight(), image.getConfig());
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(image, 0F, 0F, null);

then save new bitmap instead

柒七 2024-10-16 18:02:54

要保存图像并保留其透明区域,您不能将其保存为 JPG,而必须将其保存为 PNG,不仅如此,还要将 setHasAlpha() 的设置设置为 true 在保存图像之前,所以它会像这样:

在保存之前:

mBitmap.setHasAlpha(true);

并且在保存时,使用您用于保存的任何方法将图像保存为PNG,例如:

File file = new File(folderDir, name);
try {
    file.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}
FileOutputStream out = new FileOutputStream(file);
mBitmap.setHasAlpha(true);
mBitmap.compress(Bitmap.CompressFormat.PNG, 80, out);
out.flush();
out.close();

To save an image and retain its transparent areas you can't save it as JPG, you have to save it as PNG, and not only that, but setting the setting of setHasAlpha() to true BEFORE saving the image, so it would be like that:

before saving:

mBitmap.setHasAlpha(true);

And when saving, save the image as PNG using whatever the method you are using for saving, for example:

File file = new File(folderDir, name);
try {
    file.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}
FileOutputStream out = new FileOutputStream(file);
mBitmap.setHasAlpha(true);
mBitmap.compress(Bitmap.CompressFormat.PNG, 80, out);
out.flush();
out.close();
清旖 2024-10-16 18:02:54

对于 Kotlin,您可以使用此扩展函数

/**
 * Returns a new Bitmap with provided background color and recycles the current one.
 */
fun Bitmap.changeBackgroundColor(color: Int): Bitmap {
    val newBitmap = Bitmap.createBitmap(width, height, config)
    val canvas = Canvas(newBitmap)
    canvas.drawColor(color)
    canvas.drawBitmap(this, 0F, 0F, null)
    recycle()
    return newBitmap
}

For Kotlin you can use this Extension function

/**
 * Returns a new Bitmap with provided background color and recycles the current one.
 */
fun Bitmap.changeBackgroundColor(color: Int): Bitmap {
    val newBitmap = Bitmap.createBitmap(width, height, config)
    val canvas = Canvas(newBitmap)
    canvas.drawColor(color)
    canvas.drawBitmap(this, 0F, 0F, null)
    recycle()
    return newBitmap
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文