关于java图片切割的小demo

写此demo 的起因

         意图仿写一个flappybird,想获取flappybird的图片资源,反编译apk之后发现它的图片都被整合成了一张大图,如下

                                       

          atlas.png

       另发现一与之对应的atlas.txt文件,如图

                                     

                                              

        经研究发现,第一列为小图片文件名,第二三列为图片宽高,第四五列为图片在原图的x,y坐标的比例值,第六七列为图片宽高占原图的比例值

可以根据此文件确定小图片的基本属性,也知道小图片在大图的位置,遂开工。

       最终结果展示

                               

 

代码

            ImageBean类

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;
	}
}

            文本文件解析类

<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 文本文件路径
	 * @return list ArrayList<String []> 以字符串数组为单位的动态顺序链表
	 * */
	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();
			} catch (Exception e) {
			}
		}
		return list;
	}
}

 

     获取image集合类

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类 

       

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
 * 
 * 图片切割的小demo
 * 
 * */
public class ImageCut {
	public static void main(String[] args) throws IOException {
		//定义文本文件、初始图片路径
		String txtPath = "res/res1/atlas.txt";
		String srcpath = "res/res1/atlas.png";
		//原文件尺寸,一般都是正方形
		int imageSize = 1024;
		//剪切后图片的保存路径
		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 原文件路径
	 * @param subpath String 切割后的存盘路径
	 * @param imageBean ImageBean 
	 * 
	 * 根据原文件路径,切割后的存盘路径,imageBean ,切割图片 并保存
	 * */
	public static void cut(String srcpath, String subpath,ImageBean imageBean) throws IOException {
        FileInputStream is = null;
        ImageInputStream iis = null;
        try {
        	//ImageReader 初始化
        	ImageReader reader = (ImageReader) ImageIO
        			.getImageReadersByFormatName("png").next();
            is = new FileInputStream(srcpath);
            iis = ImageIO.createImageInputStream(is);
            reader.setInput(iis, true);
            
            //获取需要剪切的图片部分的 BufferImage 对象
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(imageBean.getX(), imageBean.getY(), imageBean.getW(), imageBean.getH());
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            
            //输出存盘位置 并存盘
            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();
        }
    }
}

项目的百度分享地址: http://pan.baidu.com/s/1bn1s6bt





相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页