png编码格式例子 alp:255 透明度(1-255) r:138 red(1-255) g:20 green(1-255) b:16 blue(1-255) -7728112 (java虚拟机中存储的int值)
1111 1111 1000 1010 0001 0100 0001 0000 补码储存在java虚拟机的二进制 1111 1111 1000 1010 0001 0100 0000 1111 补码-1 1000 0000 0111 0101 1110 1011 1111 0000 除了第一位(符号位)都取反=-7728112
现在是在图片画一个圆除了圆以外的区域都设置成透明度为100的例子 设置一个半径,半径范围以外的都设置 alp:r:g:b=255:255:255:255:255 1111 1111 1111 1111 1111 1111 1111 1111 在java里面表示0 代码
public class ParseImage {
public static void main(String[] args) throws IOException {
getMetaData("D:\home\resource\file\poster\77368221-c39e-4a1a-b640-e8546972e80emerger.png","D:\home\resource\file\poster\result.png");
}
//获取图片的原始图片信息-针对png图片
public static void getMetaData(String srcPath,String targetPath) throws IOException {
File srcImgFile = new File(srcPath);
BufferedImage image = ImageIO.read(srcImgFile);
//图片的宽度
int width = image.getWidth();
//图片的高度
int height = image.getHeight();
//图片起始点X
int minX = 0;
//图片起始点Y
int minY = 0;
double xCircle=width/2;
double xCircleDistance=xCircle*xCircle;
BufferedImage clipQrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int i = minX; i < width; i++) {
for (int j = minY; j < height; j++) {
Object data = image.getRaster().getDataElements(i, j, null);
int r = image.getColorModel().getRed(data);
int g = image.getColorModel().getGreen(data);
int b = image.getColorModel().getBlue(data);
int alp = image.getColorModel().getAlpha(data);
System.out.println("alp:"+alp);
System.out.println("r:"+r);
System.out.println("g:"+g);
System.out.println("b:"+b);
System.out.println(image.getRGB(i,j));
double distance=Math.abs((i-xCircle)*(i-xCircle))+Math.abs((j-xCircle)*(j-xCircle))+5;
if(distance>xCircleDistance){
clipQrImage.setRGB(i,j,0);
}else{
clipQrImage.setRGB(i,j, image.getRGB(i,j));
}
}
}
FileOutputStream outImgStream = new FileOutputStream(targetFile);
ImageIO.write(clipQrImage, "png", outImgStream);
outImgStream.flush();
outImgStream.close();
}
}