Java Picture Batch compression + all compression

Source: Internet
Author: User

/*

OLDSRC: Original picture address folder such as ' d:/'
NEWSRC: Compressed image address folder such as ' e:/'
Widthdist,heightdist: Width and height after compression

Createtime 2010-11-25
@auto Yijianfeng
*/

Public void Reduceimgall (String oldsrc, string newsrc, int widthdist,
int heightdist) {
try {
File File = new file (OLDSRC);
if (!file.exists ()) {
return;
   }
file[] srcfile = File.listfiles ();
if (srcfile! = null) {
for (int i = 0; i < srcfile.length; i++) {
if (Srcfile[i].isfile ()
&& (Srcfile[i].getname (). EndsWith (". jpg")
| | srcfile[i].getname (). EndsWith (". JPG ")
| | | srcfile[i].getname (). EndsWith (". gif") | | srcfile[i]
. GetName (). EndsWith (". gif"))) {
Image src = javax.imageio.ImageIO.read (srcfile[i]);
bufferedimage tag = new BufferedImage ((int) widthdist,
(int) heightdist, bufferedimage.type_int_rgb);
tag.getgraphics (). DrawImage (
src.getscaledinstance (widthdist, Heightdist,
Image.scale_smooth), 0, 0, null);
FileOutputStream out = new FileOutputStream (newsrc
+ srcfile[i].getname ());
JPEGImageEncoder encoder = Jpegcodec
. Createjpegencoder (out);
System.out.println (oldsrc + "/" + srcfile[i].getname ());
Encoder.encode (tag);
out.close ();
} else {
Reduceimgall (oldsrc + srcfile[i].getname (), NEWSRC,
widthdist, heightdist);
     }
    }
   }
} catch (IOException ex) {
ex.printstacktrace ();
  }
 }

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

Java Picture Batch compression + all compression

Related Article

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.