我最近用java缩放图片2M左右可以,但是大一点的图片总是出错
这句 ImageIO.read(file);抛出异常
网上找了很久都没有能解决的办法
请高手们帮我解决一下了。
用java代码实现,不要调内存
要求能缩小10M以上大小的图片
这句 ImageIO.read(file);抛出异常
网上找了很久都没有能解决的办法
请高手们帮我解决一下了。
用java代码实现,不要调内存
要求能缩小10M以上大小的图片
解决方案 »
- Hibernate @ManyToOne保存问题
- Hibernate save()和delete()函数不起作用的问题
- 高分求救:遇到这样的面试问题,我改怎么回答?
- 出现NotSerializableException异常怎么解决
- action方面的问题
- 问一个jsp的问题 急急!!!!求高手
- hibernate出现NoClassDefFoundError错误!急急!请大虾
- 请教高手: 如果在不重启Tomcat的情况下,用外部的方法对已经运行在Tomcat下的Web Service服务端进行一个事件激活(即传一个参数进去)?
- 散个1000分吧,当然是分次了,呵呵!
- 最近看JAVA源代码Object类里面 hashCode() 方法前面有个native修饰符 大家平时开发写方法用native修饰符吗native啥意思啊
- 请教Java项目乱码问题
- 如何用js控制radio的checked属性的值是否为true,在线等!!
InputStream imageIn = new FileInputStream(new File(imageFile));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage image1 = decoder.decodeAsBufferedImage();10M图片是能缩小了,但是颜色严重失真,就像曝光的底片。
2M图片还是跟原来一样正常缩小。
高手快来帮忙解决啊
int nw = 100;
AffineTransform transform = new AffineTransform();
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nh = (nw*h)/w ;
double sx = (double)nw/w;
double sy = (double)nh/h;
transform.setToScale(sx,sy);
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);// 更改后的文件
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",doc);
double Ratio = 0.0;
File f = new File(OriFilePath);
Image src = ImageIO.read(f);
int oriWidth = src.getWidth(null);
int oriHeight = src.getHeight(null);
int tagWidth, tagHeight;
if (oriWidth > width || oriHeight > height) {
if (oriHeight > oriWidth) {
Ratio = (new Integer(height)).doubleValue() / oriHeight;
tagHeight = height;
tagWidth = (int) (oriWidth * Ratio);
}
else {
Ratio = (new Integer(width)).doubleValue() / oriWidth;
tagHeight = (int) (oriHeight * Ratio);
tagWidth = width;
}
}
else {
tagHeight = oriHeight;
tagWidth = oriWidth;
}
BufferedImage target = new BufferedImage(tagWidth, tagHeight, BufferedImage.TYPE_INT_RGB);
target.getGraphics().drawImage(src, 0, 0, tagWidth, tagHeight, null);
FileOutputStream out = new FileOutputStream(TargetFilePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(target);
out.close();
}
调用这个方法,传四个参数:1、原图片绝对路径 2、缩略图绝对路径 3、生成缩略图后的高度 4、生成缩略图后的宽度
没试过
楼主去看看
客户不会用ps怎么啊?他们也没时间去处理啊
if(size==0){
return;
}
try {
BufferedImage src = ImageIO.read(new File(imgsrc)); // 读入文件
double width = src.getWidth(); // 得到源图宽
double height = src.getHeight(); // 得到源图长 if(width>=size||height>=size){
if(width>height){
height=height/(width/size);
width=size;
}else{
width=width/(height/size);
height=size;
}
}
reduceImg(imgsrc,imgdist,(int)width,(int)height);
}catch(Exception e){
e.printStackTrace();
}
} public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist) {
FileOutputStream out = null;
try {
File srcfile = new File(imgsrc);
if (!srcfile.exists()) {
return;
}
Image src = javax.imageio.ImageIO.read(srcfile); BufferedImage tag= new BufferedImage((int) widthdist, (int) heightdist,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null); out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); } catch (IOException ex) {
ex.printStackTrace();
}finally{
try{
if(out!=null)out.close();
}catch(Exception e){e.printStackTrace();}
}
}
File _file = new File(fullname); //读入文件
if(!_file.canRead()){
return null;
}
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
int x=src.getWidth(null); //得到源图宽
int y=src.getHeight(null);//得到源图高
float xness = (float)x;
float yness = (float)y;
if(x-y>=0){
height = (int) (width*(yness/xness));
}else{
width = (int) (height*(xness/yness));
}
//得到源图长
BufferedImage tag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH),0,0,null);//绘制缩小后的图
String _filename = _file.getName();
FileOutputStream out = new FileOutputStream(_file.getParent()+type+_filename);//输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);//近JPEG编码
String newsrc = _file.getParent()+type+_filename;
out.close();
return newsrc;
}