gpt4 book ai didi

java - 如何将 BufferedImage(RGB) 转换为 BufferedImage(ARGB 或 RGBA)?

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:33 27 4
gpt4 key购买 nike

我正在尝试将 Robot.createScreenCapture 函数生成的 RGB 图像转换为 ARGB 图像。我的目标是当两个图像之间的像素没有变化但我得到黑色时设置透明度。如下图所示。

例如如下创建的 BufferedImage:

 BufferedImage currImage = robot.createScreenCapture(new Rectangle(x, y, width,height));

以下函数旨在创建 ARGB 像素。

private BufferedImage bufferedImagesEqual(BufferedImage prevImage,
BufferedImage currImage) {
boolean changed = false;
BufferedImage imagePart = new BufferedImage(currImage.getWidth(), currImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
if (prevImage.getWidth() == currImage.getWidth()
&& prevImage.getHeight() == currImage.getHeight()) {

for (int x = 0; x < currImage.getWidth(); x++) {
for (int y = 0; y < prevImage.getHeight(); y++) {
int argb = currImage.getRGB(x, y);
if (prevImage.getRGB(x, y) != currImage.getRGB(x, y)){
// return false;
// currImage.setRGB(x, y, 0);
if(!changed){
changed = true;
}
}
else {
imagePart.setRGB(x, y, 0xff000000);
}
}
}
} else {
return imagePart;
}
if(changed){
return imagePart;
}else {
return null;
}
}

最佳答案

从之前和之后的图像开始......

Before After

通过使用 new Color(0, 0, 0, 0) 而不是 0xff000000 (并在像素相等时绘制一些东西),我得到

Output

使用...

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class ConvertBuffer {

public static void main(String[] args) {
try {
BufferedImage before = ImageIO.read(new File("/Before.jpg"));
BufferedImage after = ImageIO.read(new File("/After.jpg"));

BufferedImage result = bufferedImagesEqual(before, after);

JLabel label = new JLabel(new ImageIcon(result));
label.setOpaque(true);
label.setBackground(Color.BLUE);
JOptionPane.showMessageDialog(null, label);
} catch (Exception e) {
e.printStackTrace();
}
}

private static BufferedImage bufferedImagesEqual(BufferedImage prevImage,
BufferedImage currImage) {
boolean changed = false;
BufferedImage imagePart = new BufferedImage(currImage.getWidth(), currImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
if (prevImage.getWidth() == currImage.getWidth()
&& prevImage.getHeight() == currImage.getHeight()) {

for (int x = 0; x < currImage.getWidth(); x++) {
for (int y = 0; y < prevImage.getHeight(); y++) {
if (prevImage.getRGB(x, y) != currImage.getRGB(x, y)) {
// return false;
// currImage.setRGB(x, y, 0);
if (!changed) {
changed = true;
}
imagePart.setRGB(x, y, currImage.getRGB(x, y));
} else {
imagePart.setRGB(x, y, new Color(0, 0, 0, 0).getRGB());
}
}
}
} else {
return imagePart;
}
if (changed) {
return imagePart;
} else {
return null;
}
}

}

所以,除非您有示例图像可供使用,否则这是我们能做的最好的事情......

关于java - 如何将 BufferedImage(RGB) 转换为 BufferedImage(ARGB 或 RGBA)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26457579/

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