有一个图片在压缩之后会泛红,其他图片都正常,代码是一样的。
在网上找了很多例子,但处理这张图片还是会泛红。
通过代码调试发现:
只要用 BufferedImage srcImg = ImageIO.read(srcFile);
图片无论是否压缩,都已经泛红了。
求指导: qq 673752314
由于图片在200K以上,这里上传不上来,放到云盘上了,请大家帮忙看下
http://pan.baidu.com/s/1pJEBfp5
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
这个问题我解决了(借鉴CKFinder源码),虽然不知道什么原因,但是和大家分享下
package com.wyy.util;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ImageIcon;
public class ImageUtil {
/**
* @param srcFile 原图路径
* @param desFile 压缩图片保存路径
* @return
*/
public static void compress(String srcFile, String desFile, int outputWidth, int outputHeight){
try{
byte[] imageData = toByteArray(new FileInputStream(srcFile));
BufferedImage image = toBufferedImage(imageData);
//输入图片
File outFile = new File(desFile);
createPath(outFile, true);
resizeImage(image, outputWidth, outputHeight, 0.9f, outFile);
}catch(Exception e){
e.printStackTrace();
}
}
//把原图转换成二进制
private static byte[] toByteArray(InputStream input) {
if (input == null) {
return null;
}
ByteArrayOutputStream output = null;
byte[] result = null;
try {
output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 100];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
result = output.toByteArray();
if (output != null) {
output.close();
}
} catch (Exception e) {}
return result;
}
//把二进制转换成图片
private static BufferedImage toBufferedImage(byte[] imagedata) {
Image image = Toolkit.getDefaultToolkit().createImage(imagedata);
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
/**
* 创建输出目录
* @param file
* @param asFile
* @throws IOException
*/
private static void createPath(File file, boolean asFile) throws IOException {
String path = file.getAbsolutePath();
String dirPath;
if (asFile)
dirPath = path.substring(0, path.lastIndexOf(File.separator));
else {
dirPath = path;
}
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
if (asFile)
file.createNewFile();
}
/**
* 借助第三方工具输出图片
* @param sourceImage
* @param width
* @param height
* @param quality
* @param destFile
* @throws IOException
*/
private static void resizeImage(final BufferedImage sourceImage, final int width, final int height, final float quality, final File destFile) throws IOException {
// try {
// Thumbnails.of(sourceImage).size(width, height).keepAspectRatio(false).outputQuality(quality).toFile(destFile);
// } catch (IllegalStateException e) {
// Thumbnails.of(sourceImage).size(width, height).keepAspectRatio(false).toFile(destFile);
// }
}
}
######大佬你的demo运行不了呀###### 转换代码存在bug呗
换个稳健点的 ######遇到过,没解决,mark######谢谢,解决了我的问题。不过gif格式就不适用,出来都是黑色的######谢谢啦,解决我今天遇到的问题######大佬,你是怎么解决的