圖片實現壓縮上傳 的java類

來源:互聯網
上載者:User

package com.mobi5.commons.domain.logic;

import java.io.File;
import java.io.FileOutputStream;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

 

public class Imgproce {

 private int wideth;
   private int height;
   private String t=null;
 public void setT(String t)
   {
     this.t=t;
   }
 public void setWideth(int wideth)
   {
     this.wideth=wideth;
   }
 public int getWideth()
   {
     return this.wideth;
   }
 public void setHeight(int height)
   {
     this.height=height;
   }
 public int getHeight(int w,int h)                     //former images size
   {
     int hhh;
     if(w > wideth)
     {
       float ww;
       ww = (float) w / (float) wideth;
       float hh = h / ww;
       return (int) hh;
     }
     else
     {
       this.setWideth(w);
       return h;
     }

   }
 public void proce(String fpath) throws Exception
   {
    File _file = new File(fpath);
    System.out.println("-------"+fpath);
    Image src = javax.imageio.ImageIO.read(_file);
    int wideth=src.getWidth(null);
    int height=src.getHeight(null);
    int h=this.getHeight(wideth,height);
    BufferedImage tag = new BufferedImage(this.getWideth(),h,BufferedImage.TYPE_INT_RGB);
    Graphics g=tag.getGraphics();
    g.drawImage(src, 65, 60, this.getWideth(), h, null);
      if(t!=null)
       {
         g.setColor(new Color(242,242,242));
         g.fillRect(this.getWideth() - 120, h - 18,120,18);
         g.setColor(new Color(180,180,180));
         g.drawRect(this.getWideth() - 120, h - 18,119,17);
         g.setColor(new Color(255,102,0));
         g.drawString(t, this.getWideth() - 100, h - 5);
       }
    FileOutputStream out=new FileOutputStream(fpath);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(tag);
    out.close();
   }
 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.