Java class for compressing and uploading images

Source: Internet
Author: 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. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;

 

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 );
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (TAG );
Out. Close ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.