Java缓冲灰度图像字节和整数
请,请有人给我解释一下。我快发疯了。我正在玩用java从数组创建8位灰度图像的游戏 下面的代码应生成黑色>白色水平渐变。第1部分和第2部分确实会生成所需的图像,但在传递给Java缓冲灰度图像字节和整数,java,image,bufferedimage,grayscale,Java,Image,Bufferedimage,Grayscale,请,请有人给我解释一下。我快发疯了。我正在玩用java从数组创建8位灰度图像的游戏 下面的代码应生成黑色>白色水平渐变。第1部分和第2部分确实会生成所需的图像,但在传递给getimageFromarray的int[]中,1中的int[]从-128变为127,在2中的int[]从0变为255,但它们会生成相同的图像3生成我期望1生成的(不需要的)图像,仅使用其最大值和最小值 为什么会这样?这怎么可能 import java.awt.image.BufferedImage; import java.
getimageFromarray
的int[]中,1中的int[]从-128变为127,在2中的int[]从0变为255,但它们会生成相同的图像3生成我期望1生成的(不需要的)图像,仅使用其最大值和最小值
为什么会这样?这怎么可能
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test {
final static int WIDTH = 320;
final static int HEIGHT = 256;
// Black and white as int
final static int BLACK = 0;
final static int WHITE = 255;
//...same, but not
final static int BLACK_WRONG = 127;
final static int WHITE_WRONG = -128;
// Black and white as byte val
final static byte BLACK_BYTE = -0x80; //i.e -128
final static byte WHITE_BYTE = 0x7f; // i.e. 127
public static void main(String[] args) throws IOException {
int[] pixels = new int[WIDTH*HEIGHT];
int[] m;
// Generates gradient1.bmp
// produces as expected - black at top, white at bottom
int numb=0,c=0;
byte grey1 = BLACK;
while (numb<pixels.length){
// inc through greyscales down image
if (c>WIDTH){
grey1++;
c=0;
}
// cast from byte to int
pixels[numb] = grey1;
// inc column and count
c++;
numb++;
}
m = getMaxMin(pixels); // max 127 , min -128
System.out.printf("Maxmin %s; %s;\n",m[0], m[1]);
getImageFromArray("gradient1.bmp", pixels,WIDTH, HEIGHT);
//*************************************************************************
// Generates gradient3.bmp
// produces as expected - black at top, white at bottom
numb=0;
c=0;
int grey2 = BLACK; //i.e zero
while (numb<pixels.length){
// inc through greyscales down image
if (c>WIDTH){
grey2++;
c=0;
}
// no cast
pixels[numb] = grey2;
// inc column and count
c++;
numb++;
}
m = getMaxMin(pixels); // max 255, min 0
System.out.printf("Maxmin %s; %s;\n",m[0], m[1]);
getImageFromArray("gradient2.bmp", pixels,WIDTH, HEIGHT);
//*************************************************************************
// Generates gradient3.bmp
// produces as unexpected - midgrey > white. black > midgrey
numb=0;
c=0;
byte grey3 = BLACK_BYTE; //i.e zero
while (numb<pixels.length){
// inc through greyscales down image
if (c>WIDTH){
grey3++;
c=0;
}
// no cast
pixels[numb] = grey3;
// inc column and count
c++;
numb++;
}
m = getMaxMin(pixels); // max 127 , min -128
System.out.printf("Maxmin %s; %s;\n",m[0], m[1]);
getImageFromArray("gradient3.bmp", pixels,WIDTH, HEIGHT);
}
//*******************************************************************************
static int sWidth,sHeight = 0;
static BufferedImage sImage = null;
static WritableRaster sRaster=null;
public static BufferedImage getImageFromArray(String filename, int pixels[], int width, int height) throws IOException {
if (sImage == null){
sImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
sRaster = sImage.getRaster();
}
sRaster.setPixels(0,0,width,height,pixels);
try {
ImageIO.write(sImage, "bmp", new FileOutputStream(filename));
} catch (IOException e) {
e.printStackTrace();
}
return sImage;
}
static int[] getMaxMin(int[] v){
int max=0,min=0;
for (int i:v){
if (i>max) max = i;
if (i<min) min = i;
}
int[] r = {max,min};
return r;
}
}
导入java.awt.image.buffereImage;
导入java.awt.image.WritableRaster;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入javax.imageio.imageio;
公开课考试{
最终静态整数宽度=320;
最终静态整数高度=256;
//黑白相间
最终静态整数黑色=0;
最终静态整数白=255;
//……相同,但不同
最终静态整数黑色错误=127;
最终静态整数白_错=-128;
//黑白相间
最终静态字节BLACK_byte=-0x80;//即-128
最终静态字节WHITE_byte=0x7f;//即127
公共静态void main(字符串[]args)引发IOException{
int[]像素=新int[宽度*高度];
int[]m;
//生成gradient1.bmp
//按预期生产-顶部为黑色,底部为白色
int numb=0,c=0;
字节grey1=黑色;
while(宽度){
grey1++;
c=0;
}
//从字节转换为int
像素[numb]=灰色1;
//inc列和计数
C++;
麻木++;
}
m=getMaxMin(像素);//最大127,最小-128
System.out.printf(“最大值%s;%s;\n”,m[0],m[1]);
getImageFromArray(“gradient1.bmp”,像素、宽度、高度);
//*************************************************************************
//生成gradient3.bmp
//按预期生产-顶部为黑色,底部为白色
麻木=0;
c=0;
int grey2=黑色;//即零
while(宽度){
grey2++;
c=0;
}
//无演员阵容
像素[numb]=灰色2;
//inc列和计数
C++;
麻木++;
}
m=getMaxMin(像素);//最大255,最小0
System.out.printf(“最大值%s;%s;\n”,m[0],m[1]);
getImageFromArray(“gradient2.bmp”,像素、宽度、高度);
//*************************************************************************
//生成gradient3.bmp
//产生意外-中灰色>白色。黑色>中灰色
麻木=0;
c=0;
字节灰色3=黑色字节//i、 零度
while(宽度){
grey3++;
c=0;
}
//无演员阵容
像素[numb]=灰色3;
//inc列和计数
C++;
麻木++;
}
m=getMaxMin(像素);//最大127,最小128
System.out.printf(“最大值%s;%s;\n”,m[0],m[1]);
getImageFromArray(“gradient3.bmp”,像素、宽度、高度);
}
//*******************************************************************************
静态整数开关,开关高度=0;
静态BuffereImage sImage=null;
静态可写光栅sRaster=null;
公共静态缓冲区图像getImageFromArray(字符串文件名,整数像素[],整数宽度,整数高度)引发IOException{
if(sImage==null){
sImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_BYTE_GRAY);
sRaster=sImage.getRaster();
}
sRaster.setPixels(0,0,宽度,高度,像素);
试一试{
write(sImage,“bmp”,新文件输出流(filename));
}捕获(IOE异常){
e、 printStackTrace();
}
返回sImage;
}
静态int[]getMaxMin(int[]v){
int max=0,min=0;
对于(int i:v){
如果(i>max)max=i;
如果(i假设BufferedImage
采用RGBA(或类似)编码颜色作为输入,则需要从各自的RGBA组件正确构造这些int
s
要将有符号字节R=0
,G=0
,B=0
,A=127
转换为灰色,您必须将它们转换为无符号int
并将它们合并为一个int
,如下所示:
int color = (((A & 0xff) << 24 | ((B & 0xff) << 16) | ((G << 8) | (R & 0xff));
int color=((A&0xff)thx表示(sole!)回答。是的,buffereImage
被设置为buffereImage。键入_BYTE\u GRAY
这样每像素只取一个值,但是当我转到RGBA的东西时,它很有用。通过快速谷歌,我认为你提到的无符号和有符号int/字节中有一些东西。我会调查一下,看看这是否是我困惑的根源。