使用Java,分割大图.(把大图里的小图分割出来 可以用来 分割 在网上下载的 做帧动画的大图 plist )

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * 图形识别技术
 * 时间:2019-4-6
 * */
public class ImageUtil {


    private static List<String> emptyPoint = new ArrayList<>();

    public static void plistPngImage(String sourcesFilePath) {

        try {
            if(!new File(sourcesFilePath).exists()){
                return;
            }
            int amount = 1;
            File sourceFile = new File(sourcesFilePath);
            String newFilePath = sourceFile.getParentFile()+"\\"+sourceFile.getName().split("\\.")[0];
            new File(newFilePath).mkdirs();
            BufferedImage bi =  ImageIO.read(new File(sourcesFilePath));

            //获取图像的宽度和高度
            int width = bi.getWidth();
            int height = bi.getHeight();

            int beginX = 0;
            for(int i=0;i<width;i++){
                Integer cutY_1 = null;
                Integer cutY_2 = null;
                for(int j = 0; j < height; j++){
                    if(!linePointIsEmpty_x(bi,j,beginX,i)){
                        if(cutY_1 == null){
                            cutY_1 = j;
                        }
                    }else{
                        if(cutY_1 != null){
                            cutY_2 = j;
                            Integer cutX_1 = null;
                            Integer cutX_2 = null;
                            for(int k=beginX;k<=i;k++){
                                if(!linePointIsEmpty_y(bi,k,cutY_1,cutY_2)){
                                    if(cutX_1 == null){
                                        cutX_1 = k;
                                    }
                                }else{
                                    if(cutX_1 != null){
                                        cutX_2 = k;

                                        System.out.println("开始截图点为:("+cutX_1+","+cutY_1+"),结束截图的点为:("+cutX_2+","+cutY_2+")"+i);
                                        cutPNG(sourcesFilePath,newFilePath+"\\"+(amount++)+".png", cutX_1,cutY_1,(cutX_2-cutX_1)+1,(cutY_2-cutY_1)+1);

                                        addPointToEmpty(bi,cutX_1,cutY_1,cutX_2,cutY_2);

                                        cutX_1 = null;
                                    }
                                }
                            }
                            cutY_1 = null;
                        }
                    }
                }

                for(int h=beginX;h<i;h++){
                    if(linePointIsEmpty_y(bi,h,0,height-1)){
                        beginX = h;
                        for(int l=0;l<emptyPoint.size();l++){
                            if(Integer.parseInt(emptyPoint.get(l).split("-")[0])<beginX){
                                emptyPoint.remove(l);
                            }
                        }
                        System.out.println(emptyPoint.size());
                    }else{
                        break;
                    }
                }
                System.out.println(beginX);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    private static boolean pointIsEmpty(BufferedImage bi,Integer x,Integer y){

        int p = bi.getRGB(x, y);

        if(p == 0xffffff){
            return true;
        }
        if(p == 0){
            return true;
        }
        if(emptyPoint.contains(x+"-"+y)){
            return true;
        }
        return false;
    }

    private static boolean linePointIsEmpty_x(BufferedImage bi,Integer y,Integer x_1,Integer x_2){

        for(int j=x_1;j<=x_2;j++){
            if(!pointIsEmpty(bi,j,y)){
                return false;
            }
        }
        return true;
    }
    private static boolean linePointIsEmpty_y(BufferedImage bi,Integer x,Integer y_1,Integer y_2){
        for(int i=y_1;i<=y_2;i++){
            if(!pointIsEmpty(bi,x,i)){

                return false;
            }
        }
        return true;
    }
    private static void addPointToEmpty(BufferedImage bi,Integer x_1,Integer y_1,Integer x_2,Integer y_2){
        for(int i=x_1;i<=x_2;i++){
            for(int j=y_1;j<=y_2;j++){
                int p = bi.getRGB(i,j);
                if(p == 0xffffff){
                    continue;
                }
                if(p == 0){
                    continue;
                }
                if(!emptyPoint.contains(i+"-"+j)){
                    emptyPoint.add(i+"-"+j);
                }
            }
        }
    }

    public static void cutJPG(String sourceFilePath, String newFilePath, int x, int y, int width, int height) throws IOException {
        ImageInputStream imageStream = null;
        try {
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
            ImageReader reader = readers.next();
            imageStream = ImageIO.createImageInputStream(new FileInputStream(sourceFilePath));
            reader.setInput(imageStream, true);
            ImageReadParam param = reader.getDefaultReadParam();

            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            if(!new File(newFilePath).exists()){
                new File(newFilePath).createNewFile();
            }
            ImageIO.write(bi, "jpg", new FileOutputStream(newFilePath));
        } finally {
            imageStream.close();
        }
    }


    public static void cutPNG(String sourceFilePath, String newFilePath, int x, int y, int width, int height) throws IOException {
        ImageInputStream imageStream = null;
        try {
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("png");
            ImageReader reader = readers.next();
            imageStream = ImageIO.createImageInputStream(new FileInputStream(sourceFilePath));
            reader.setInput(imageStream, true);
            ImageReadParam param = reader.getDefaultReadParam();

            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            if(!new File(newFilePath).exists()){
                new File(newFilePath).createNewFile();
            }
            ImageIO.write(bi, "png", new FileOutputStream(newFilePath));
        } finally {
            imageStream.close();
        }
    }

    public static void cutImage(String sourceFilePath, String newFilePath, String type,int x, int y, int width, int height) throws IOException {
        ImageInputStream imageStream = null;
        try {
            String imageType=(null==type||"".equals(type))?"jpg":type;
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageType);
            ImageReader reader = readers.next();
            imageStream = ImageIO.createImageInputStream(new FileInputStream(sourceFilePath));
            reader.setInput(imageStream, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            if(!new File(newFilePath).exists()){
                new File(newFilePath).createNewFile();
            }
            ImageIO.write(bi, imageType, new FileOutputStream(newFilePath));
        } finally {
            imageStream.close();
        }
    }


    public static void main(String[] args) {
        plistPngImage("e:\\111.png");
    }


}

全部代码就是这了,,,  只要把大图路径写对 就能分割了,分割后的路径 和大图路径一样.    因为是一个像素一个像素分析,所以  效率可能不是那么快    大家有兴趣可以留言,,看如何增加以下效率   

猜你喜欢

转载自blog.csdn.net/q18792880831/article/details/100013591
今日推荐