Java recoloring BuffereImage无法处理更高的图像
我有一个程序,它应该获取图像的RGB值,然后将它们乘以一些常数,然后在JPanel上绘制新图像。问题是,如果我的图像超过一定高度,特别是超过187像素,则新的彩色图像与高度小于187px的图像不同 JPanel显示了以下内容: 请注意,较长的重新存储图像与较短的图像有多么不同。我确信较短的图像的颜色是正确的,我不知道它是怎么搞砸的Java recoloring BuffereImage无法处理更高的图像,java,rgb,bufferedimage,Java,Rgb,Bufferedimage,我有一个程序,它应该获取图像的RGB值,然后将它们乘以一些常数,然后在JPanel上绘制新图像。问题是,如果我的图像超过一定高度,特别是超过187像素,则新的彩色图像与高度小于187px的图像不同 JPanel显示了以下内容: 请注意,较长的重新存储图像与较短的图像有多么不同。我确信较短的图像的颜色是正确的,我不知道它是怎么搞砸的 public class RecolorImage extends JPanel { public static int scale = 3; public sta
public class RecolorImage extends JPanel {
public static int scale = 3;
public static BufferedImage walk, walkRecolored;
public static BufferedImage shortWalk, shortWalkRecolored;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200*scale, 400*scale);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RecolorImage());
walk = ImageLoader.loadImage("/playerWalk.png");
walkRecolored = recolor(walk);
shortWalk = ImageLoader.loadImage("/playerWalkShort.png");
shortWalkRecolored = recolor(shortWalk);
frame.setVisible(true);
}
@Override
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
g.scale(scale, scale);
g.drawImage(walk, 10, 10, null);
g.drawImage(walkRecolored, 40, 10, null);
g.drawImage(shortWalk, 70, 10, null);
g.drawImage(shortWalkRecolored, 100, 10, null);
}
回收方法:
public static BufferedImage recolor(BufferedImage image) {
BufferedImage outputImage = deepCopy(image);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
Color c = new Color(rgb);
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
r *= 0.791;
g *= 0.590;
b *= 0.513;
int newRGB = (rgb & 0xff000000) | (r << 16) | (g << 8) | b;
outputImage.setRGB(x, y, newRGB);
}
}
return outputImage;
}
我的原始图像:和。谢谢你的帮助 源图像具有不同的颜色模型:
- 短图像每像素使用4个字节(RGB和alpha)
- 高图像每像素使用1字节(索引到256色调色板)
deepCopy
方法),因此高图像的重新着色图像也使用与源图像相同的调色板,这意味着它不能包含您想要的所有颜色
由于重新着色代码会覆盖输出图像的每个像素,因此不需要进行深度复制操作。相反,您最好创建一个全彩图像作为目标图像,如下所示:
public static BufferedImage recolor(BufferedImage image) {
BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
//... code as before
}
public static BufferedImage recolor(BufferedImage image) {
BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
//... code as before
}