我遇到这个问题,是因为在微信公众号网页版对图片进行编辑时出现的问题,主要是IOS上传的图片,在图片编辑时出现的问题,为什么出现这个问题,原因是识别不了这张图片。如下原码:
public BufferedImage(int width,
int height,
int imageType) {
switch (imageType) {
case TYPE_INT_RGB:
{
colorModel = new DirectColorModel(24,
0x00ff0000, // Red
0x0000ff00, // Green
0x000000ff, // Blue
0x0 // Alpha
);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_INT_ARGB:
{
colorModel = ColorModel.getRGBdefault();
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_INT_ARGB_PRE:
{
colorModel = new
DirectColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
32,
0x00ff0000,// Red
0x0000ff00,// Green
0x000000ff,// Blue
0xff000000,// Alpha
true, // Alpha Premultiplied
DataBuffer.TYPE_INT
);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_INT_BGR:
{
colorModel = new DirectColorModel(24,
0x000000ff, // Red
0x0000ff00, // Green
0x00ff0000 // Blue
);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_3BYTE_BGR:
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
width, height,
width*3, 3,
bOffs, null);
}
break;
case TYPE_4BYTE_ABGR:
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
int[] bOffs = {3, 2, 1, 0};
colorModel = new ComponentColorModel(cs, nBits, true, false,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
width, height,
width*4, 4,
bOffs, null);
}
break;
case TYPE_4BYTE_ABGR_PRE:
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
int[] bOffs = {3, 2, 1, 0};
colorModel = new ComponentColorModel(cs, nBits, true, true,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
width, height,
width*4, 4,
bOffs, null);
}
break;
case TYPE_BYTE_GRAY:
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {8};
colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_USHORT_GRAY:
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {16};
colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_BYTE_BINARY:
{
byte[] arr = {(byte)0, (byte)0xff};
colorModel = new IndexColorModel(1, 2, arr, arr, arr);
raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE,
width, height, 1, 1, null);
}
break;
case TYPE_BYTE_INDEXED:
{
// Create a 6x6x6 color cube
int[] cmap = new int[256];
int i=0;
for (int r=0; r < 256; r += 51) {
for (int g=0; g < 256; g += 51) {
for (int b=0; b < 256; b += 51) {
cmap[i++] = (r<<16)|(g<<8)|b;
}
}
}
// And populate the rest of the cmap with gray values
int grayIncr = 256/(256-i);
// The gray ramp will be between 18 and 252
int gray = grayIncr*3;
for (; i < 256; i++) {
cmap[i] = (gray<<16)|(gray<<8)|gray;
gray += grayIncr;
}
colorModel = new IndexColorModel(8, 256, cmap, 0, false, -1,
DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
width, height, 1, null);
}
break;
case TYPE_USHORT_565_RGB:
{
colorModel = new DirectColorModel(16,
DCM_565_RED_MASK,
DCM_565_GRN_MASK,
DCM_565_BLU_MASK
);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
case TYPE_USHORT_555_RGB:
{
colorModel = new DirectColorModel(15,
DCM_555_RED_MASK,
DCM_555_GRN_MASK,
DCM_555_BLU_MASK
);
raster = colorModel.createCompatibleWritableRaster(width,
height);
}
break;
default:
throw new IllegalArgumentException ("Unknown image type " +
imageType);
}
this.imageType = imageType;
}
注意看最后的异常信息,就是我们要找的报错信息。
既然找到了异常的问题,那我们就对应下,如下图。下图中框中的部分需要我们自定义图像,那我们就自定义吧
解决思路,我的问题是图片的位深度为64时报这个错,那么我思路就是降低它的位深度
实现逻辑:将TYPE_CUSTOM转成TYPE_3BYTE_RGB,继而重新生成一张图片,删除原来的图片。
弊端:问题虽然可以解决了,但是图片变小了,这个我目前解决不了
接下来是实现代码
package com.happydayin.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
// private void changJPG(File uploadFile) throws IOException {
// String path = uploadFile.getPath();
// // TODO Auto-generated method stub
// BufferedImage bufferedImage= ImageIO.read(uploadFile);
// // create a blank, RGB, same width and height, and a white background
// BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
// bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
// //TYPE_INT_RGB:创建一个RBG图像,24位深度,成功将32位图转化成24位
// newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
// // write to jpeg file
// String fileName = path.substring(0,path.lastIndexOf("."));
// ImageIO.write(newBufferedImage, "jpg", new File(fileName+".jpg"));
//
// }
public static void main(String[] args) throws IOException {
File uploadFile = new File("D:\\picdata\\1.jpg");
String path = uploadFile.getPath();
// TODO Auto-generated method stub
BufferedImage bufferedImage= null;
try {
bufferedImage = ImageIO.read(uploadFile);
} catch (IOException e) {
e.printStackTrace();
}
// create a blank, RGB, same width and height, and a white background
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
bufferedImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
//TYPE_INT_RGB:创建一个RBG图像,24位深度,成功将32位图转化成24位
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
// write to jpeg file
String fileName = path.substring(0,path.lastIndexOf("."));
ImageIO.write(newBufferedImage, "jpg", new File(fileName+"3.jpg"));
}
}
问题1:安卓上传的照片即使是64位深度,也可以操作,苹果却不可以,没找到原因
问题2:给图片自定义时,图片变小了,无法解决
如果你看到这个帖子,刚好你解决了这两个问题,请给我留言,告诉我解决办法。谢谢