About the Java picture cutting small demo

Source: Internet
Author: User
write the cause of this demo

Intent to write a flappybird, want to get flappybird of the picture resources, decompile apk later found that its pictures have been integrated into a large picture, as follows


Atlas.png

Another found a corresponding atlas.txt file, such as the figure


After the study found that the first column as a small picture file name, the 23rd column of the picture width high, the 45th column in the original image of the x,y coordinates of the scale value, 67th column picture width of the original proportion of the value of

Can be based on this file to determine the basic properties of small pictures, but also know the small picture in the position of the large map, and then started.

Final results show



Code

Imagebean class

public class Imagebean {
	private String name;
	private int x;
	private int y;
	private int w;
	private int h;
	
	Public Imagebean (String name, int x, int y, int w, int h) {
		this.name = name;
		this.x = x;
		This.y = y;
		THIS.W = W;
		This.h = h;
	}
	
	Public String GetName () {return
		name;
	}

	public int GetX () {return
		x;
	}

	public int GetY () {return
		y;
	}

	public int getw () {return
		w;
	}

	public int Geth () {return
		h;
	}

	@Override public
	String toString () {return
		name+ "" +x+ "" +y+ "" +w+ "" +H;
	}
}

Text file Resolution Class

<pre name= "code" class= "Java" >import java.io.File; import java.io.FileInputStream; import
Java.util.ArrayList;

Import Java.util.Scanner;
	 public class Txtutil {/** * @param txtpath String text file path * @return List arraylist<string []> a dynamic sequential list of string arrays
		* */public static arraylist<string[]> getarraylist (String txtpath) {arraylist<string []> list = null;
		File file = null;
		FileInputStream inputstream = null;
		Scanner Scanner = null;
			
			try {list = new arraylist<> ();
			File = new file (Txtpath);
			InputStream = new FileInputStream (file);
			
			Scanner = new Scanner (InputStream);
				while (Scanner.hasnextline ()) {String lineString = Scanner.nextline ();
				String[] result = Linestring.split ("");
			List.add (result);
		} catch (Exception e) {e.printstacktrace ();
				Finally {try {inputstream.close ();
			Scanner.close ();
	The catch (Exception e) {}} return list; }
}

Get the Image Collection class

Import java.util.ArrayList;

public class Imageutil {public
	
	static arraylist<imagebean> getimagebeans (int imagesize,string txtpath) {
		arraylist<imagebean> Imagebeans = new arraylist<> ();
		arraylist<string []> list = txtutil.getarraylist (Txtpath);
		
		For (string[] strings:list) {
			String name = Strings[0];
			int x = (int) (Imagesize*float.parsefloat (strings[3]));
			int y = (int) (Imagesize*float.parsefloat (strings[4]));
			int w = (int) (Imagesize*float.parsefloat (strings[5]));
			int h = (int) (Imagesize*float.parsefloat (strings[6]));
			
			Imagebeans.add (New Imagebean (name,x,y,w,h));
		}
		Return Imagebeans
	}
	
}

Imagecut class

Import Java.awt.Rectangle;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;

Import java.util.ArrayList;
Import Javax.imageio.ImageIO;
Import Javax.imageio.ImageReadParam;
Import Javax.imageio.ImageReader;

Import Javax.imageio.stream.ImageInputStream;  /** * @author Guxingke * * * * * * * Image Cut Demo * * */public class Imagecut {public static void main (string[] args) throws
		IOException {//Definition text file, initial picture path String Txtpath = "Res/res1/atlas.txt";
		String Srcpath = "Res/res1/atlas.png";
		The original file size, is generally square int imagesize = 1024;
		
		The saved path of the cut image String subpath = "result/cut1/";
		
		arraylist<imagebean> Imagebeans = Imageutil.getimagebeans (Imagesize,txtpath);
		for (Imagebean Imagebean:imagebeans) {cut (Srcpath, subpath, Imagebean);
	} System.out.println ("Success"); /** * @param srcpath String Original file path * @param subpath string after the cut disk path * @param imagebean Imagebean * * According to the original file Path, after cutting the disk path, IMAgebean, cut pictures and save */public static void Cuts (string srcpath, String Subpath,imagebean Imagebean) throws IOException {
        FileInputStream is = null;
        Imageinputstream IIS = NULL; try {//imagereader initializes imagereader reader = (imagereader) ImageIO. Getimagereadersbyformatna
            Me ("PNG"). Next ();
            is = new FileInputStream (Srcpath);
            IIS = Imageio.createimageinputstream (IS);
            
            Reader.setinput (IIS, True);
            Gets the Bufferimage object imagereadparam param = Reader.getdefaultreadparam () for the part of the picture that needs to be clipped.
            Rectangle rect = new Rectangle (Imagebean.getx (), Imagebean.gety (), IMAGEBEAN.GETW (), Imagebean.geth ());
            Param.setsourceregion (rect);
            
            BufferedImage bi = reader.read (0, param);
            The output disk location coexists with the disc System.out.println (Subpath+imagebean.getname () + ". png"); Imageio.write (BI, "PNG", New File (Subpath+imagebean.getname () + ". png")));
        catch (Exception e) {e.printstacktrace ();
            }finally {if (is!= null) is.close ();
        if (IIS!= null) Iis.close (); }
    }
}

Baidu share of the project address: HTTP://PAN.BAIDU.COM/S/1BN1S6BT





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.