Java Traversal folder and image scaling batch processing

Source: Internet
Author: User

Remember I used to write the image processing of the article is used in the teaching of digital image processing to operate, using a MATLAB, but the recent internship and a sister paper a little conflict, both sides have not much time to deal with these resources, and then I was quiet to think about, sister paper how to spend so much time in this work.

Then ran to communicate, understand her entire work flow, sure enough to find, she modified the image size is a picture to a change, the whole process spent 1 hours, the heart brainwave, batch processing certainly can write, previously done similar, but sister paper no MATLAB. In order to liberate the productive forces, I still have to drive things out in the evening.


First of all, the provisions of the document, there are several files, each file has a JPG and PNG image format, you need to put the specifications of JPG to 600*400 specifications, for horizontal screen picture, for vertical screen picture, will be resolution interchange, PNG format picture into 200*200.


The first thing is to specify a folder, put the previously specified folder in.

File F = new file ("E://console"), if (!f.exists ()) {   f.mkdir ();}

The console folder specified as the E drive. With this folder, if the folder does not have files, it is necessary to give a sister paper awake.

if (f.listfiles () = = null) {  System.out.println ("No Game in E://consolegame directory");}

After you have this directory, traverse the file and look for the png-suffix file.

For (File everyfile:game.listFiles ()) {if (Everyfile.getname (). split ("\ \") [1].equals ("PNG")) {<span style= "font-size:18px;" >}</span><p><span style= "font-size:18px;" >}</span></p>
Split inside the parameter is a regular expression, the point is to match the meaning of a single character, so you need to escape the character \, and carefully will find that the windows inside the path is a backslash, in fact, the input oblique access can also be accessed. The backslash is not directly recognized as an escape character in Java, so it needs to be written in \ \.

Divide the file name and file type name into arrays, and then remove the required files:


try {System.out.println (Chinese);       BufferedImage the Read method for the subclass of image ImageIO returns to bufferedimage  bufferedimage image = Imageio.read (everyfile);  system.out.println (chinese+ "icon png file has been scaled to x*x specification");//System.out.println (Image.getwidth ());//        System.out.println (Image.getheight ()); To create a scaled version of the image, the first two parameters are the image width and height, and finally the image sampling algorithm//image.scale_smooth is a more priority algorithm for image smoothness than scaling speed//scale_fast the scaling speed is more priority than image smoothness        FA  image newimage = Image.getscaledinstance (Pngwidth, Pngheight, Image.scale_smooth); Constructs an image, the last parameter is an image color component of RGB  bufferedimage board = new BufferedImage (Pngwidth, Pngheight, BUFFEREDIMAGE.TYPE_INT_RG        B);        The method returns GRAPHICS2D, as a subclass of the Graphics, in fact with creategraphics more convenient, for backward compatibility considerations//For drawing images  graphics g = board.getgraphics (); The first parameter is the image that needs to be drawn, the second and third parameters are X and y coordinates, the third parameter is a//background color drawn for the opaque portion of the image, and the last object to be notified of the image being converted (This is not very understanding)  g.drawimage (Newi        Mage,0,0,color.white,null);        Release resources  g.dispose (); Delete the original file   Everyfile.delete (); Imageio.write (board, "PNG", New File ("e://console//" +everyfile.getname ())); catch (IOException e) {e.printstacktrace ();}

Image processing part, did not expect to use Java processing than MATLAB simple, after all, Matlab is their own directly to operate each pixel.


The rest of the JPG format, but the processing to be based on vertical screen picture or horizontal screen picture to adjust separately, the horizontal screen can not be squashed, vertical screen of the pressure.

if (Everyfile.getname (). split ("\ \") [1].equals ("jpg")) {try {bufferedimage image = Imageio.read (everyfile); image newimage = null; BufferedImage board = null;if (Image.getwidth () >image.getheight ()) {newimage = Image.getscaledinstance (Jpgwidth, Jpgheight, image.scale_smooth); board = new BufferedImage (Jpgwidth, Jpgheight, Bufferedimage.scale_smooth); System.out.println (Everyfile.getname () + "Footage jpg file has been scaled to x*x");} Else{newimage = Image.getscaledinstance (jpgheight,jpgwidth,image.scale_smooth); board = new BufferedImage (Jpgheight, Jpgwidth,bufferedimage.scale_smooth); System.out.println (Everyfile.getname () + "Footage jpg file has been scaled to x*x");} Graphics g = Board.getgraphics (); G.drawimage (newimage,0,0,null); G.dispose (); Everyfile.delete (); Imageio.write ( Board, "JPG", New File ("e://console//" +everyfile.getname ()));} catch (IOException e) {e.printstacktrace ();}

The principle is the same as above.


The final sense of accomplishment overflowing, sister paper productivity liberation, in fact, as long as the computer on the operation, programming things can be used basically. That night, I was giggling and walking home. Yes, programmers will sigh at their own things to do good.


But Java has the limitations of Java, when I compiled version than sister paper run version of the time, the problem comes, this problem, or in C + + better.

Java Traversal folder and image scaling batch processing

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.