java按比例压缩图片的源代码,用java如何把图片处理到指定大小

用java如何把图片处理到指定大小

前言:


朋友在做图片切割的时候遇到一个问题:如何用java如何把图片处理到指定大小?
切割程序如下:
public void cut(String srcImageFile,FileOutputStream fileout, int w, int h, int x1,
int y1, int sw, int sh) {
// TODO Auto-generated method stub
try {

// http://localhost:8080/ImpCra/createServlet?p=Sunset.jpg&x=117&y=201&w=61&h=50&pw=300&ph=400

Image img;

ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));

if (sw >= w && sh >= h) {

Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);

// 剪切起始坐标点
int x = x1;
int y = y1;

int destWidth = w; // 切片宽度
int destHeight = h; // 切片高度

// 图片比例
double pw = sw;
double ph = sh;

double m = (double) sw / pw;
double n = (double) sh / ph;
System.out.println(m);
int wth = (int) (destWidth * m);
int hth = (int) (destHeight * n);
int xx = (int) (x * m);
int yy = (int) (y * n);

// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)

cropFilter = new CropImageFilter(xx, yy, wth, hth);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), cropFilter));

BufferedImage tag = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();

// 输出为文件
ImageIO.write(tag, "JPEG", fileout);

}

} catch (Exception e) {
e.printStackTrace();
}
}

进入正题:
解决方案参考源码,java按比例压缩图片的源代码如下:


public static void uploadImage(File p_in,File p_out,int height,int width,String ftype) throws FileNotFoundException,IOException
   {
//    取得图片处理
//    ConvertImageFactory l_factory = ConvertImageFactory.getInstance();
//    AbstractProduct l_product = l_factory.createAbstractProduct(p_SourceFile.getContentType());
//    boolean l_result = l_product.convertImageSize(p_SourceFile
//      .getInputStream(), p_path );
    InputStream l_in = new FileInputStream(p_in);
    OutputStream l_out = new FileOutputStream(p_out);
    chgPic(l_in,l_out,width,height,ftype);
  
  
   }
 
   //按比例压缩图片
   public static boolean chgPic(InputStream in, OutputStream out,int newWidth, int newHeight,String ftype) {
    BufferedImage img = null;
    FileInputStream newin=null;
    File tempfile=null;
    try {
     if(ftype.compareToIgnoreCase("bmp")==0){
      PNGDecodeParam decodeParam = new PNGDecodeParam();
      String l_tempfile = Tool.createNewFileName("jpg");
    
      tempfile = new File(l_tempfile);
      JPEGEncodeParam encodeParam = new JPEGEncodeParam();
      //根据路径打开输出流
      FileOutputStream tempout;
      tempout = new FileOutputStream(tempfile);  
      ImageDecoder decoder = ImageCodec.createImageDecoder("BMP",in,decodeParam);
      RenderedImage image = decoder.decodeAsRenderedImage();
      ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG",tempout,encodeParam);
      encoder.encode(image);
      tempout.close();
    
      newin = new FileInputStream(tempfile);
      img = ImageIO.read(newin);
     }else{
      img = ImageIO.read(in);
     }
   
     int width = img.getWidth(null);
     int height = img.getHeight(null);
   
     if (newWidth >= width) {
      if (newHeight < height) {
       width = (int) (width * newHeight / height);
       height = newHeight;
      }
     } else {
      if (newHeight >= height) {
       height = (int) (height * newWidth / width);
       width = newWidth;
      } else {
       if (height > width) {
        width = (int) (width * newHeight / height);
        height = newHeight;
       } else {
        height = (int) (height * newWidth / width);
        width = newWidth;
       }
      }
     }
   
     BufferedImage img2 = new BufferedImage(width, height,
       BufferedImage.TYPE_INT_RGB);
     img2.getGraphics().drawImage(img, 0, 0, width, height, null);
   
     if (ftype.compareToIgnoreCase("jpg") == 0 || ftype.compareToIgnoreCase("jpeg") == 0 ) {
      ImageIO.write(img2, "jpg", out);
     } else
      ImageIO.write(img2, "png", out);
   
     if( ftype.compareToIgnoreCase("bmp") == 0){
      ImageIO.write(img2, "jpg", out);
      newin.close();
      tempfile.delete();
     }
     return true;
    } catch (Exception ex) {
     ex.printStackTrace();
     return false;
    }finally{
     try{
      in.close();
      out.close();
     } catch (IOException e) {
    
     }  
    }
   }

你可能感兴趣的:(java按比例压缩图片的源代码,用java如何把图片处理到指定大小)