我想将图像颜色量化从16位减少到8位。到目前为止这是我的代码。它只会使图像透明。根据我所学到的,我应该采用每个像素并除以比特尺寸减小的因子。我在这里做错了什么?
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.util.Scanner;
public class ConvertImage{
public static void main(String args[])throws IOException{
BufferedImage image = null;
File f = null;
try {
f = new File("lennapng.png");
image = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}
//get width and height
int width = image.getWidth();
int height = image.getHeight();
width = image.getWidth();
height = image.getHeight();
for(int y = 0; y < height; y++) {
for(int x = 0; x <height; x++) {
int p = image.getRGB(x, y);
// System.out.println("rgb "+ rgb);
int a = (p>>24) & 0xff;
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = p&0xff;
r= r*16/256;
g= g*16/256;
b = b*16/256;
//set new RGB
p = (a<<12) | (r<<8) | (g<<4) | b;
image.setRGB(x, y, p);
}
}
try{
f = new File("output.png");
ImageIO.write(image, "png", f);
}catch(IOException e){
System.out.println(e);
}
}
}