我一直在尝试制作一个Java程序,该程序将允许某些像素的颜色更改为中的其他颜色BufferedImage
,但是正在绘制的颜色似乎覆盖了旧的颜色。这就是我的意思:
这是当前在.java文件中的代码:
static BufferedImage image = null;
static File file = null;
static int height;
static int width;
static int[][][] pixelStorage;
static int pixel;
static int getPixelDataOutput = 0;
static Random random = new Random();
public static void main(String args[]) throws IOException {
System.out.println("test");
try {
file = new File("C:\\Users\\kkosy\\Dev\\Java\\Random\\Images - Color Changer\\Images\\input.png");
}catch(Exception exception) {
System.out.println(exception);
}
image = ImageIO.read(file);
height = image.getHeight();
width = image.getWidth();
pixelStorage = new int[height][width][4];
for(int h = 0; h < height - 1; h++) {
for(int w = 0; w < width - 1; w++) {
savePixelData(w, h);
if(getPixelData(w,h,"r") == 0){
System.out.println();
printPixelData(w,h,"Before -- ");
setPixelData(w,h,255,0,0,255);
printPixelData(w,h,"After -- ");
}
}
}
try {
file = new File("C:\\Users\\kkosy\\Dev\\Java\\Random\\Images - Color Changer\\Images\\output.png");
ImageIO.write(image, "jpg", file);
}catch(Exception exception){
System.out.println(exception);
}
}
private static void savePixelData(int x, int y) {
pixel = image.getRGB(x,y);
pixelStorage[y][x][0] = (pixel >> 24) & 0xff;
pixelStorage[y][x][1] = (pixel >> 16) & 0xff;
pixelStorage[y][x][2] = (pixel >> 8) & 0xff;
pixelStorage[y][x][3] = pixel & 0xff;
//printPixelData(x,y,"");
}
private static void setPixelData(int x, int y, int alpha, int red, int green, int blue) {
int setPixel = (alpha << 24) | (red << 16) | (green << 8) | (blue);
image.setRGB(x, y, setPixel);
image.setRGB(x, y, new Color(red,green,blue).getRGB());
}
private static void printPixelData(int x, int y, String arguments) {
System.out.println(arguments + "" + pixelStorage[y][x][0] + " " + pixelStorage[y][x][1] + " " + pixelStorage[y][x][2] + " " + pixelStorage[y][x][3] + " ");
}
private static int getPixelData(int x, int y, String argb) {
switch(argb) {
case "a": {
getPixelDataOutput = pixelStorage[y][x][0];
break;
}
case "r": {
getPixelDataOutput = pixelStorage[y][x][1];
break;
}
case "g": {
getPixelDataOutput = pixelStorage[y][x][2];
break;
}
case "b": {
getPixelDataOutput = pixelStorage[y][x][3];
break;
}
}
return getPixelDataOutput;
}
我不知道为什么它会输出这样的图像。也许setRGB()
是这些方面的。
您正在尝试创建PNG文件,但此行...
ImageIO.write(image, "jpg", file);
...正在尝试编写JPEG。根据Unixfile
命令,输出确实是JPEG ...,也带有暗青色像素的坏情况。
当我换"jpg"
到"png"
,我得到了一个output.png
看起来完全一样的文件input.png
。
我不知道您希望输出看起来像什么(并且我使用的是不同的输入文件),但这似乎比JPEG-in-PNG色衣服的颜色更近。
您的日志对于调试将毫无用处,因为printPixelData
始终会打印中的值pixelStorage
。由于setPixelData
仅更改中的值image
,因此您将始终打印两次“之前”值,而永远不会打印“之后”值。
顺便说一句,所有这些静态变量使跟踪程序的执行比必须的要难得多。至少将您可以使用的方法移动到方法中(例如height
,width
和file
,这些方法从未在之外使用main
),然后删除根本不用的方法(例如getPixelDataOutput
和random
)。
可能无关,但是该setPixelData
方法设置了一个像素,然后立即将其重置为其他值:
private static void setPixelData(
int x, int y,
int alpha, int red, int green, int blue
) {
int setPixel = (alpha << 24) | (red << 16) | (green << 8) | (blue);
image.setRGB(x, y, setPixel);
// Why overwrite the value you just set?
image.setRGB(x, y, new Color(red,green,blue).getRGB());
}
这似乎并没有改变任何东西---(无论是否有第一次调用setRGB
---我的测试图像看起来都一样,但可能不是您想要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句