Java version of the album Production Program

Source: Internet
Author: User

Import Java.awt.Graphics;


import Java.awt.Image;


import Java.awt.image.BufferedImage;


import Java.awt.image.ImageObserver;


import Java.io.File;


import Java.io.FileFilter;


import Java.io.FileInputStream;


import Java.io.FileOutputStream;


import Java.io.FileWriter;


import java.io.IOException;


import Java.io.InputStream;


import Com.sun.image.codec.jpeg.JPEGCodec;


import Com.sun.image.codec.jpeg.JPEGImageDecoder;


import Com.sun.image.codec.jpeg.JPEGImageEncoder;


public class Zoom {


String Srcpath;


StringBuffer html;


int count;


public Zoom (String srcpath) {


This.srcpath = Srcpath;


init ();


}


public void Zoom (File input) {


//Position of output


String output = Getoutputpath ();


try {


InputStream imagestream = new FileInputStream (input);


//Create a cached picture based on the target picture


Jpegimagedecoder decoderfile = Jpegcodec.createjpegdecoder (ImageStream);


BufferedImage imagefile = Decoderfile.decodeasbufferedimage ();


float zoom = 0.12F; You want to square the proportion


//Get the width of the target picture, and times the scaling ratio to get the new picture size


int w = (int) (Imagefile.getwidth () * zoom);


int h = (int) (Imagefile.getheight () * zoom);


//Create a cached picture of a new picture


bufferedimage bufimage = new BufferedImage (W, H, Bufferedimage.type_int_rgb);


String zoomfile = output + "/zooms_" + input.getname ();


FileOutputStream out = new FileOutputStream (zoomfile);


//From the target picture to get graphics to draw on the new picture, the last parameter is an internal nameless class, you can use NULL instead of


Graphics g = bufimage.getgraphics ();


G.drawimage (imagefile, 0, 0, W, H, new ImageObserver () {


public boolean imageupdate (Image img, int infoflags, int x, int y, int width, int height) {


return true;


}


});


//encoded output


jpegimageencoder jpeg = Jpegcodec.createjpegencoder (out);


Jpeg.encode (bufimage);


Out.flush ();


Out.close ();


Imagestream.close ();


int row = count% 3;


if (row = = 0) {


html.append ("/n/t<tr>");


}


html.append ("/n/t/t<td align= ' center ' ><a href= '"). Append (Input.getname ()). Append ("' target= ' _blank ')" > ");


html.append ("<img src= ' Zoom" + "/zooms_" + input.getname () + "' border= ' 0 ' ><br>");


Html.append (Input.getname () + "</a></td>");


if (row = = 2) {


html.append ("/n/t</tr>");


}


count++;


} catch (Exception e) {


E.printstacktrace ();


}


}


public void process () {


file[] files = GetFiles ();


mkdirs ();


for (int i = 0; i < files.length; i++) {


Zoom (files[i]);


}


Trail ();


Outputhtmlfile ();


}


Private file[] GetFiles () {


file Path = new file (Srcpath);


file[] files = path.listfiles (new FileFilter () {


public Boolean accept (File pathname) {


if (pathname = null)


return false;


String ext = pathname.getname (). substring (Pathname.getname (). LastIndexOf (".") + 1). toUpperCase ();


return Ext.equals ("JPG") | | Ext.equals ("JPEG");


}


});


return files;


}


private void Mkdirs () {


file Zoompath = new file (Getoutputpath ());


Zoompath.mkdirs ();


}


private String Getoutputpath () {


return Srcpath + "/zoom";


}


private void Init () {


count = 0;


html = new StringBuffer ();


html.append ("<html>");


html.append ("/n<head>");


html.append ("/n<meta http-equiv=/" content-type/"content=/" text/html; charset=gb2312/">");


html.append ("/n<title>"). Append (Getdirname ()). Append ("</title>");


html.append ("/n</head>");


html.append ("/n/n<body>");


html.append ("/n<table width= ' 75% ' border= ' 1 ' >");


}


private void Trail () {


int row = count% 3;


if (row = = 0) {


Html.append ("/n/t/t<td> </td>");


}


if (row = = 1) {


html.append ("/n/t/t<td> </td>");


html.append ("/n/t/t<td> </td>");


}


html.append ("/n/t</tr>");


html.append ("/n</table>");


html.append ("/n</body>");


html.append ("/n</html>");


}


private String Getdirname () {


if (Srcpath.endswith ("/")) {


Srcpath = srcpath.substring (0, Srcpath.length ()-1);


}


return srcpath.substring (Srcpath.lastindexof ("/") + 1);


}


private void Outputhtmlfile () {


FileWriter writer = null;


try {


file Htmlfile = new file (Srcpath + "/index.html");


writer = new FileWriter (Srcpath + "/index.html");


Writer.write (Html.tostring ());


Writer.flush ();


} catch (IOException e) {


E.printstacktrace ();


} finally {


if (writer!= null) {


try {


Writer.close ();


} catch (IOException E1) {


E1.printstacktrace ();


}


}


}


}


public static void Main (string[] args) {


String srcpath = args[0];


if (srcpath==null) {


Printhelp ();


return;


}


Zoom Zoom = new Zoom (Srcpath);


zoom.process ();


}


public static void Printhelp () {


System.out.println ("Usage:java Zoom <FILEPATH>");


}


}

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.