JAVA迅速解决图片变红、变黑、压缩平滑问题
2013-12-26 12:29
597 查看
小卒 最近在做头像上传功能,发现对于带有alpha通道的图片,有的会变红,有的会变黑,有时压缩不平滑。发现是有几处程序写错了。
首先贴一下原始的程序,这段程序的功能是将原图的(x,y,w,h)图块,转变为 (0,0,targetW,targetH)图片,并输出为jpg格式
private byte[] cropAndCompressImage(int x, int y, int w, int h,
BufferedImage image, int targetW, int targetH) throws Exception{
if (w == -1 && h == -1 && image != null) {
// use full image
w = image.getWidth();
h = image.getHeight();
}
if (x < 0 || y < 0 || w <= 0 || h <= 0 || image == null) {
// error
}
w = Math.min(x+w, image.getWidth()) - x;
h = Math.min(y+h, image.getHeight()) - y;
try {
BufferedImage cropedImage = image.getSubimage(x, y, w, h);
// do image compression
int type = image.getType() == 0 ? BufferedImage.TRANSLUCENT
: image.getType();
BufferedImage compressedImage = new BufferedImage(targetW, targetH, type);
Graphics2D graph = compressedImage.createGraphics();
graph.drawImage(cropedImage, 0, 0, targetW, targetH, null);
graph.dispose();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ImageIO.write(compressedImage, "jpg", bytes);
return bytes.toByteArray();
} catch (IOException e) {
// error
}
}首先是背景有时变黑,这里最简单的做法是将最后的背景图片设置为白色。见下面代码: //graph.drawImage(cropedImage, 0, 0, targetW, targetH, null);
graph.drawImage(cropedImage, 0, 0, targetW, targetH, Color.white,null);
加一个Color.white即可。下面要解决的是图片变红问题。帐前卒认为这个问题其实是alpha通道需要变为透明的。而原始程序是判断,如果该图片是定义为Custom的图片,那么就变为透明。显然custom的图片不一定是透明图片。所以这里需要判断image是否为透明的。代码变为:// int type = image.getType() == 0 ? BufferedImage.TRANSLUCENT
: image.getType();
// BufferedImage compressedImage = new BufferedImage(targetW, targetH, type);
BufferedImage compressedImage = null;
if (image.isAlphaPremultiplied()) {
compressedImage = new BufferedImage(photoType.getWidth(), photoType.getHeight(), BufferedImage.TRANSLUCENT);
} else {
compressedImage = new BufferedImage(photoType.getWidth(), photoType.getHeight(), BufferedImage.TYPE_INT_RGB);
}
然后变红问题就解决了。下面是压缩图片时,发现很多地方不光滑,这里也非常好解决。帐 前卒的代码如下:
加入SCALE_SMOOTH参数就完成了~~
首先贴一下原始的程序,这段程序的功能是将原图的(x,y,w,h)图块,转变为 (0,0,targetW,targetH)图片,并输出为jpg格式
private byte[] cropAndCompressImage(int x, int y, int w, int h,
BufferedImage image, int targetW, int targetH) throws Exception{
if (w == -1 && h == -1 && image != null) {
// use full image
w = image.getWidth();
h = image.getHeight();
}
if (x < 0 || y < 0 || w <= 0 || h <= 0 || image == null) {
// error
}
w = Math.min(x+w, image.getWidth()) - x;
h = Math.min(y+h, image.getHeight()) - y;
try {
BufferedImage cropedImage = image.getSubimage(x, y, w, h);
// do image compression
int type = image.getType() == 0 ? BufferedImage.TRANSLUCENT
: image.getType();
BufferedImage compressedImage = new BufferedImage(targetW, targetH, type);
Graphics2D graph = compressedImage.createGraphics();
graph.drawImage(cropedImage, 0, 0, targetW, targetH, null);
graph.dispose();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ImageIO.write(compressedImage, "jpg", bytes);
return bytes.toByteArray();
} catch (IOException e) {
// error
}
}首先是背景有时变黑,这里最简单的做法是将最后的背景图片设置为白色。见下面代码: //graph.drawImage(cropedImage, 0, 0, targetW, targetH, null);
graph.drawImage(cropedImage, 0, 0, targetW, targetH, Color.white,null);
加一个Color.white即可。下面要解决的是图片变红问题。帐前卒认为这个问题其实是alpha通道需要变为透明的。而原始程序是判断,如果该图片是定义为Custom的图片,那么就变为透明。显然custom的图片不一定是透明图片。所以这里需要判断image是否为透明的。代码变为:// int type = image.getType() == 0 ? BufferedImage.TRANSLUCENT
: image.getType();
// BufferedImage compressedImage = new BufferedImage(targetW, targetH, type);
BufferedImage compressedImage = null;
if (image.isAlphaPremultiplied()) {
compressedImage = new BufferedImage(photoType.getWidth(), photoType.getHeight(), BufferedImage.TRANSLUCENT);
} else {
compressedImage = new BufferedImage(photoType.getWidth(), photoType.getHeight(), BufferedImage.TYPE_INT_RGB);
}
然后变红问题就解决了。下面是压缩图片时,发现很多地方不光滑,这里也非常好解决。帐 前卒的代码如下:
// graph.drawImage(cropedImage, 0, 0, targetW, targetH, null); // graph.drawImage(cropedImage, 0, 0, targetW, targetH, Color.white,null); graph.drawImage(cropedImage.getScaledInstance(targetW, targetH, BufferedImage.SCALE_SMOOTH), 0, 0, Color.white,null);
加入SCALE_SMOOTH参数就完成了~~
相关文章推荐
- Java裁剪压缩PNG图片,透明背景色变黑问题解决
- 解决java压缩图片透明背景变黑色的问题
- 解决java压缩图片透明背景变黑色的问题
- 使用java实现多种方式解决图片压缩的问题
- 解决java压缩图片透明背景变黑色的问题
- 解决java 图片压缩图片图片变色问题
- Ubuntu/Debian下JAVA 中文/图片中文字体乱码显示问题解决方法
- 使用java向MySQL存储图片遇到的问题及解决
- 使用Java上传图片经过压缩处理图片变色问题
- 最新Javascript按比例压缩图片,解决onload无法读取图片的高度和宽度问题
- 将图片作为窗体背景,但是显示不出的问题解决。java。
- 异常-----springmvc + ajaxfileupload解决ajax不能异步上传图片的问题。java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.
- 解决调用系统拍照保存的图片被压缩的问题
- ionic或者angularjs中图片显示压缩问题解决 or 显示较大图片的某一块区域、裁剪显示
- Ubuntu/Debian下JAVA 中文/图片中文字体乱码显示问题解决方法
- 一些手机图片压缩处理后会出现旋转问题(三星)的解决方法
- Android Glide白色图片压缩变绿的问题解决
- 【Java】Java根据路径获取图片偶尔会得不到图片问题的解决方法
- android 加载大图片是出现java.lang.OutOtMemoryError问题解决方法
- js,angularjs 图片上传服务器,并解决照片旋转问题,前端图片压缩上传