我遇到這個問題,是因爲在微信公衆號網頁版對圖片進行編輯時出現的問題,主要是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:給圖片自定義時,圖片變小了,無法解決
如果你看到這個帖子,剛好你解決了這兩個問題,請給我留言,告訴我解決辦法。謝謝