当前位置:编程学习 > JAVA >>

java压缩图片 异常 坐等

压缩文件 a8b9172de123999ac44ff7690948da6e.jpg 出此异常     压缩 05067e5b4dc26e760c3bd98946f28e81.jpg 无异常

Exception in thread "main" java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match
at java.awt.image.ColorConvertOp.filter(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at abc.ImageZipUtil.zipImageFile(ImageZipUtil.java:43)
at abc.ImageZipUtil.main(ImageZipUtil.java:100)


package abc;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageZipUtil {

/**
 * 压缩图片文件<br>
 * 先保存原文件,再压缩、上传
 * 
 * @param oldFile
 *            要进行压缩的文件全路径
 * @param width
 *            宽度
 * @param height
 *            高度
 * @param quality
 *            质量
 * @param smallIcon
 *            小图片的后缀
 * @return 返回压缩后的文件的全路径
 */
public static String zipImageFile(String oldFile, int width, int height,
float quality, String smallIcon) {
if (oldFile == null) {
return null;
}
String newImage = null;
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(new File(oldFile));
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
/** 压缩后的文件名 */
newImage = filePrex + smallIcon
+ oldFile.substring(filePrex.length());

/** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newImage);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}

/**
 * 保存文件到服务器临时路径
 * 
 * @param fileName
 * @param is
 * @return 文件全路径
 */
public static String writeFile(String fileName, InputStream is) {
if (fileName == null || fileName.trim().length() == 0) {
return null;
}
try {
/** 首先保存到临时文件 */
FileOutputStream fos = new FileOutputStream(fileName);
byte[] readBytes = new byte[512];// 缓冲大小
int readed = 0;
while ((readed = is.read(readBytes)) > 0) {
fos.write(readBytes, 0, readed);
}
fos.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
public static void main(String args[]){
ImageZipUtil.zipImageFile("e:\\pic\\a8b9172de123999ac44ff7690948da6e.gif", 85, 117, 0.5f, "x");

}
}



--------------------编程问答--------------------

这是无异常图片 --------------------编程问答-------------------- 。。。。。。。路过、路过。 --------------------编程问答-------------------- 有人么 --------------------编程问答-------------------- ColorConvertOp
public ColorConvertOp(ICC_Profile[] profiles,
                      RenderingHints hints)
Constructs a new ColorConvertOp from an array of ICC_Profiles. The RenderingHints argument may be null. The sequence of profiles may include profiles that represent color spaces, profiles that represent effects, etc. If the whole sequence does not represent a well-defined color conversion, an exception is thrown. 
For BufferedImages, if the ColorSpace of the source BufferedImage does not match the requirements of the first profile in the array, the first conversion is to an appropriate ColorSpace. If the requirements of the last profile in the array are not met by the ColorSpace of the destination BufferedImage, the last conversion is to the destination's ColorSpace. 

For Rasters, the number of bands in the source Raster must match the requirements of the first profile in the array, and the number of bands in the destination Raster must match the requirements of the last profile in the array. The array must have at least two elements or calling the filter method for Rasters will throw an IllegalArgumentException. 


Parameters:
profiles - the array of ICC_Profile objects
hints - the RenderingHints object used to control the color conversion, or null 
Throws: 
IllegalArgumentException - when the profile sequence does not specify a well-defined color conversion 
NullPointerException - if profiles is null

现在只能说个大概,原图片中raster bands数和color space components数不匹配
由具体哪些参数关联指定还不清楚,可能是大小,可能是少设置了什么参数(需要查阅一些资料才能知道),或者可能的话可以跟进源码中看看,尤其是java.awt.image.ColorConvertOp.filter方法 --------------------编程问答-------------------- 我是来看广告的。。。。。 --------------------编程问答-------------------- 是不是源图像类型的目标图像类型不一致所致?试试:

/** 对服务器上的临时文件进行处理 */
BufferedImage srcFile = ImageIO.read(new File(oldFile));
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height, srcFile.getType());
--------------------编程问答-------------------- 是不是源图像类型和目标图像类型不一致所致? --------------------编程问答-------------------- 在这句话: Image srcFile = ImageIO.read(new File(oldFile));里找不到或不能识别OldFile,你需要确定文件的路径是否正确和类型是否正确 --------------------编程问答-------------------- 图片的问题,重新编辑一下或换一下后缀
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,