java图片失真问题

藍調↘ヅ孤獨 发布于 2013/11/10 22:06
阅读 2K+
收藏 3

package com.image.opper;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.ImageIcon;

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

public class ImageOpper {
 
 public BufferedImage getBufferImage(File file) throws Exception{
  ImageReader reader = ImageIO.getImageReadersByFormatName("jpeg").next(); 
  reader.setInput(new FileImageInputStream(file), true, false); 
  return reader.read(0); 
 }
 
 public ImageIcon getImageIcon(File file){
  return new ImageIcon(file.getAbsolutePath());
 }
 
 /**
     *  通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
     * @author lifeng
     * @param targetWidth 目标的宽度
     * @param targetHeight 目标的高度
     * @param standardWidth 标准(指定)宽度
     * @param standardHeight 标准(指定)高度
     * @return 最小的合适比例
     */
    public  double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) {
        double widthScaling = 0d;
        double heightScaling = 0d;
        if (targetWidth > standardWidth) {
            widthScaling = standardWidth / (targetWidth * 1.00d);
        } else {
            widthScaling = 1d;
        }
        if (targetHeight > standardHeight) {
            heightScaling = standardHeight / (targetHeight * 1.00d);
        } else {
            heightScaling = 1d;
        }
        return Math.min(widthScaling, heightScaling);
    }
 
 /**
  * 图片缩小
  * @param image 原始图片
  * @param width  新图片的宽度
  * @param heigth 新图片的高度
  * @param isSame 是否按比例缩放
  * @return
  */
 public BufferedImage zoom(BufferedImage image,int width,int height,boolean isSame){
  if(!isSame){
   BufferedImage new_image = new BufferedImage(width, height, image.getType());
   Graphics g = new_image.getGraphics();
      g.drawImage(image, 0,0,width,height,null);
      g.dispose();
      return new_image;
  }else{
   double times = getScaling(image.getWidth(),image.getHeight(),width,height);
   if(times < 0)
    times = times * -1;
   int new_width = 0;
   int new_height = 0;
   if(width > image.getWidth() && height > image.getHeight()){
    new_width = (int) (image.getWidth()/times);
    new_height = (int)(image.getHeight()/times);
   }else{
    new_width = (int)(image.getWidth()*times);
    new_height = (int)(image.getHeight()*times);
   }
   
   BufferedImage newImage = new BufferedImage(new_width,new_height,image.getType());
   Graphics g = newImage.getGraphics();
   g.drawImage(image.getScaledInstance(width, height,  Image.SCALE_SMOOTH), 0,0,new_width,new_height,null);
   g.dispose();
   return newImage;
  }
 }
 
 public String save_image(BufferedImage image,String save_path,String file_type) throws Exception{
  File dir_file = new File(save_path);
  if(!dir_file.exists())
   dir_file.mkdirs();
  Image rescaled = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_AREA_AVERAGING);
  String fileName = UUID.randomUUID().toString();
  File file = new File(save_path+"//"+fileName+"."+file_type);
  BufferedImage output = toBufferedImage(rescaled, image.getType());
  FileOutputStream fos = new FileOutputStream(file);
  
        JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fos);
        JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(output);
        jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
        jpegEncodeParam.setXDensity(350);
        jpegEncodeParam.setYDensity(350);
        jpegEncoder.encode(output, jpegEncodeParam);
   fos.close();
  
  return file.getAbsolutePath();
 }
 
 
 public static BufferedImage toBufferedImage(Image image, int type) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        BufferedImage result = new BufferedImage(w, h, type);
        Graphics2D g = result.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return result;
    }
 
 
 public BufferedImage gen_image(BufferedImage image,int width,int height,Color backcolor){
  
  if(image.getWidth() == width && image.getHeight() == height){
   return image;
  }else{
   int cut_size = (width - image.getWidth())/2;
   if(cut_size < 0)
    cut_size = cut_size * -1;
   int cut_height = (height - image.getHeight())/2;
   if(cut_height < 0)
    cut_height = cut_height * -1;
   BufferedImage new_image = new BufferedImage(width, height, image.getType());
   Graphics2D dg = (Graphics2D) new_image.createGraphics();
   dg.setColor(backcolor);//设置笔刷白色
   dg.fillRect(0,0,width,height);//填充整个屏幕
   dg.dispose();
   Graphics g = new_image.getGraphics();
   g.drawImage(image, cut_size,cut_height,image.getWidth(),image.getHeight(),null);
   g.dispose();
   return new_image;
  }
 }
 
   
    public static void main(String[] args) throws Exception {
     ImageOpper opper = new ImageOpper();
     BufferedImage image = opper.getBufferImage(new File("F:\\15c44e07-41b3-4793-85ea-dbe24c67c2c9.jpg"));
     int rgb = image.getRGB(1, 1);
     BufferedImage new_image = opper.zoom(image, 800, 800, true);
     Color color = new Color(rgb);
     BufferedImage result_imag = opper.gen_image(new_image, 800, 800, color);
     opper.save_image(result_imag, "D:\\image_test", "jpg");
 }
}


经过上面的代码处理后深色的图片变亮了,5M的图片处理后也只有100多K,这个是什么原因,要怎么处理

加载中
0
z
zhongxh
5M的图片肯定不止800*800了,你自己进行了缩小,文件自然也小了;用白色填充那个dg貌似是费代码?color参数也没用到,不知何意。
0
藍調↘ヅ孤獨

引用来自“zhongxh”的答案

5M的图片肯定不止800*800了,你自己进行了缩小,文件自然也小了;用白色填充那个dg貌似是费代码?color参数也没用到,不知何意。
肯定用到了 dg.setColor(backcolor);这个备注是有点问题,是获取第一个像素点的颜色作为背景色
藍調↘ヅ孤獨
回复 @zhongxh : 你把那段代码去掉背景就是黑色的了
z
zhongxh
你只是“用过”而已,联系上下文,dg就没有任何作用,直接dispose销毁了,说画蛇添足都不算,纯粹没有任何意义。
返回顶部
顶部