我正在尝试使用ImageIO读取jpg,然后在调整图像大小后将其写为PNG。
这是我的代码:
BufferedImage img = ImageIO.read(new FileInputStream("/u/my.jpg"));
BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = out.createGraphics();
g.drawImage(img, 0, 0, img.getWidth()/2, img.getHeight()/2, null);
g.dispose();
ImageIO.write(out, "png", new FileOutputStream("/u/my2.png"));
我遇到的问题是这导致PNG-8881(二进制/位掩码Alpha层)。我想最终得到一个PNG-8888,其中8位用于alpha而不是1位用于alpha。
这是Image Magick识别输出:
Image: /u/my2.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 2848x4288+0+0
Units: Undefined
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Red:
min: 0 (0)
max: 252 (0.988235)
mean: 42.2154 (0.165551)
standard deviation: 84.6142 (0.33182)
kurtosis: 1.41395
skewness: 1.77533
Green:
min: 0 (0)
max: 252 (0.988235)
mean: 45.1979 (0.177247)
standard deviation: 86.0758 (0.337552)
kurtosis: 0.935575
skewness: 1.62013
Blue:
min: 0 (0)
max: 255 (1)
mean: 46.4455 (0.182139)
standard deviation: 87.2088 (0.341995)
kurtosis: 0.742603
skewness: 1.56331
Alpha:
min: 0 (0)
max: 255 (1)
mean: 63.75 (0.25)
standard deviation: 110.418 (0.433013)
kurtosis: -0.666667
skewness: -1.1547
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 81.2772 (0.318734)
standard deviation: 92.6906 (0.363492)
kurtosis: 0.642375
skewness: 1.36843
Alpha: none #00000000
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 2848x4288+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2014-01-06T12:15:38-06:00
date:modify: 2014-01-06T12:15:38-06:00
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 2848, 4288
png:sRGB: intent=0 (Perceptual Intent)
signature: f1cd61ef11890cb9981e30787ee13f9a27e0836b4b634fa7b9daf2f6bb14de66
Artifacts:
filename: /u/my2.png
verbose: true
Tainted: False
Filesize: 3.321MB
Number pixels: 12.21M
Pixels per second: 55.51MB
User time: 0.210u
Elapsed time: 0:01.219
Version: ImageMagick 6.8.6-6 2013-10-12 Q16 http://www.imagemagick.org
我需要PNG是一个32位PNG文件,全8位alpha。我该怎么做呢?
谢谢。
好的,所以在阅读了@haraldK的一条评论之后。
更新代码:
public static void main(String[] args) throws IOException{
BufferedImage img = ImageIO.read(new FileInputStream("/u/my.jpg"));
BufferedImage out = new BufferedImage(img.getWidth()/2, img.getHeight()/2, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = out.createGraphics();
g2D.drawImage(img, 0, 0, img.getWidth()/2, img.getHeight()/2, null);
int w = out.getWidth();
int h = out.getHeight();
g2D.dispose();
WritableRaster ras = out.getColorModel().createCompatibleWritableRaster(out.getWidth(), out.getHeight());
BufferedImage out2 = new BufferedImage(out.getColorModel(), ras, false, null);
ColorModel model = out.getColorModel();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int data = out.getRGB(j, i);
int pixel = model.getRGB(data);
int a = pixel >> 24 & 0xff;
int r = pixel >> 16 & 0xff;
int g = pixel >> 8 & 0xff;
int b = pixel & 0xff;
Color color = new Color(r, g, b, a);
if(i==25 && j==25){
out2.setRGB(j, i, new Color(111,111,111,126).getRGB());
}else{
out2.setRGB(j, i, pixel);
}
}
}
ImageIO.write(out2, "png", new FileOutputStream("/u/my126.png"));
}
来自运行的新输出识别图像文件:
Image: /u/my126.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 1424x2144+0+0
Units: Undefined
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 8-bit
Channel statistics:
Red:
min: 34 (0.133333)
max: 252 (0.988235)
mean: 168.862 (0.662203)
standard deviation: 85.1618 (0.333968)
kurtosis: -1.63932
skewness: -0.419596
Green:
min: 42 (0.164706)
max: 252 (0.988235)
mean: 180.791 (0.708986)
standard deviation: 71.5681 (0.280659)
kurtosis: -1.51545
skewness: -0.454524
Blue:
min: 44 (0.172549)
max: 255 (1)
mean: 185.782 (0.728557)
standard deviation: 67.3447 (0.264097)
kurtosis: -1.44714
skewness: -0.500635
Alpha:
min: 126 (0.494118)
max: 255 (1)
mean: 255 (1)
standard deviation: 0.0738282 (0.000289522)
kurtosis: 3.05305e+06
skewness: 1747.3
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 133.859 (0.524936)
standard deviation: 65.0189 (0.254976)
kurtosis: 5.00426
skewness: -0.456343
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 1424x2144+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2014-01-06T17:08:21-06:00
date:modify: 2014-01-06T17:08:21-06:00
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 1424, 2144
png:sRGB: intent=0 (Perceptual Intent)
signature: 8bb6e37dbde980af0296dfacebff27b217360aac8994dfad8e90efeecac3bbf2
Artifacts:
filename: /u/my126.png
verbose: true
Tainted: False
Filesize: 3.01MB
Number pixels: 3.053M
Pixels per second: 20.35MB
User time: 0.090u
Elapsed time: 0:01.150
Version: ImageMagick 6.8.6-6 2013-10-12 Q16 http://www.imagemagick.org
基本上就是这样。当识别为所有阿尔法值只读取255时 - 它错误地标记了图像文件8881,但是如果我将一个像素值更改为具有任意透明度梯度 - 识别将PNG文件正确识别为8888。
答案 0 :(得分:0)
identify
输出似乎存在一些不一致之处。这让我相信identify
是错误的(或者至少没有显示我们期望的结果),并且图像中的alpha通道确实是8位(如果你查看“Channel statistics”, Alpa max是255,255必须是8位值。
这似乎与您的进一步调试一致。因此,结论:图像是正确的32位PNG,8位alpha。 : - )