如何使用java创建16位灰度图像
我试图创建一个16位PNG,但无法得到它一直写黑色。另外,如何将定义为255255/r、g、b的8位颜色转换为16位颜色如何使用java创建16位灰度图像,java,image-processing,Java,Image Processing,我试图创建一个16位PNG,但无法得到它一直写黑色。另外,如何将定义为255255/r、g、b的8位颜色转换为16位颜色 BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_USHORT_GRAY); // 65536 for (int i = 0; i < 256; i++) for (int j = 0; j < 256; j++) {
BufferedImage bi = new BufferedImage(256, 256,
BufferedImage.TYPE_USHORT_GRAY);
// 65536
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++) {
int mask = 0xf0
int value = 255 & mask; // zero other bits
value >>= 16;
bi.setRGB(i, j, value);
// bi.setRGB(i, j, 65536);
}
File f = new File("gray.png");
try {
ImageIO.write(bi, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
buffereImage bi=新的buffereImage(256,
BuffereImage.TYPE_-USHORT_-GRAY);
// 65536
对于(int i=0;i<256;i++)
对于(int j=0;j<256;j++){
int mask=0xf0
int value=255&mask;//其他位为零
值>>=16;
bi.setRGB(i,j,值);
//bi.setRGB(i,j,65536);
}
文件f=新文件(“gray.png”);
试一试{
ImageIO.write(bi,“png”,f);
}捕获(IOE异常){
e、 printStackTrace();
}
行值>>=16
正在将其设置为零
至于从24位RGB到16位颜色的转换,通常有两种方法。。。RGB565和RGB555。数字表示每个颜色分量的位数。不是解决此问题的方法,而是实现要求的另一种方法
BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_USHORT_GRAY);
Graphics2D g = bi.createGraphics();
g.setColor(Color.GRAY);
g.fillRect(0,0,256,256);
File f = new File("C:/gray.png");
try {
ImageIO.write(bi, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
对不起,我不是通过我的电脑,但你能告诉我这是创建一个8位还是16位的png吗?谢谢@用户1088777我不知道。这是一个相关的问题,可能会有所帮助。