java多线程分隔图片程序_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java多线程分隔图片程序

java多线程分隔图片程序

 2013/7/12 18:14:57  xyzxingyan  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.xyz.test;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Toolkit;importjava.awt.image.BufferedImage;importjava.awt.image.CropImageFilter;importjava.awt.image.FilteredImageSource;importjava.awt.image.ImageFilter;importjava.io
  • 标签:程序 图片 多线程 Java 线程
class="java" name="code">package com.xyz.test;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageCut {

	/**
	 * 图像切割 改用线程
	 * @param srcBi源图像
	 * @param desImgName 切片目标文件名
	 * @param desType 目标图片类型jpg、png
	 * @param desWidth 目标切片宽度
	 * @param desHeight 目标切片高度
	 */
	@SuppressWarnings("unused")
	private void cutImag(BufferedImage srcBi, String desImgName, String desType, int desWidth, int desHeight) {
		try {
			int srcWidth = srcBi.getWidth(); // 源图宽度
			int srcHeight = srcBi.getHeight(); // 源图高度
			if (srcWidth > desWidth && srcHeight > desHeight) {
				Image image = srcBi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH);
				int cols = 0; // 切片横向数量
				int rows = 0; // 切片纵向数量
				int lastRW = srcWidth % desWidth;
				int lastRH = srcHeight % desHeight;
				// 计算切片的横向和纵向数量
				if (lastRW == 0) {
					cols = srcWidth / desWidth;
				} else {
					cols = (int) Math.floor(srcWidth / desWidth) + 1;
				}
				
				if (lastRH == 0) {
					rows = srcHeight / desHeight;
				} else {
					rows = (int) Math.floor(srcHeight / desHeight) + 1;
				}
				
				// 循环建立切片
				// 改进的想法:是否可用多线程加快切割速度
				BufferedImage tagBi = null;
				Graphics g = null;
				Image img;
				ImageFilter cropFilter; // 读取源图像
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						// 四个参数分别为图像起点坐标和宽高
						cropFilter = new CropImageFilter(j * desWidth, i * desHeight, lastRW > 0 && cols - 1 == j ? lastRW : desWidth, lastRH > 0 && rows - 1 == i ? lastRH : desHeight);
						img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
						tagBi = new BufferedImage(lastRW > 0 && cols - 1 == j ? lastRW : desWidth, lastRH > 0 && rows - 1 == i ? lastRH : desHeight, BufferedImage.TYPE_INT_RGB);
						g = tagBi.getGraphics();
						g.drawImage(img, 0, 0, null); // 绘制缩小后的图
						g.dispose();
						// 输出为文件
						ImageIO.write(tagBi, desType, new File(desImgName + "_" + BaseDir.formartNumber(j) + "_" + BaseDir.formartNumber(i) + "."+desType));
					}
				}
			}else{
				ImageIO.write(srcBi, desType, new File(desImgName + "_000_000."+desType));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 为nanojs框架切割图片
	 * @param srcImgFile 要切割的文件名称
	 * @param descImgName 目标文件名称
	 * @param tilesize 目标图片类型jpg、png
	 * @param tilesize 分隔大小 
	 */
	public void cutImagForPanojs(String srcImgFile, String descImgName, String descType, int tilesize){
		try {
			BufferedImage srcBi = ImageIO.read(new File(srcImgFile)); // 读入文件
			int level_width = srcBi.getWidth();    
			int level_height = srcBi.getHeight();   
			int i = 1;
			Image image = null;
			BufferedImage tagBi = null;
			Graphics g = null;
			//切割原始图片
			//this.cutImag(srcBi, descImgName + BaseDir.formartNumber(0), descType, tilesize, tilesize);
			//改用线程
			new CutImgThread(srcBi, descImgName + BaseDir.formartNumber(0), descType, tilesize, tilesize).start();
		    while (level_width > tilesize || level_height > tilesize) {      
		    	level_width  = (int) Math.floor(level_width / 2);
		        level_height = (int) Math.floor(level_height / 2);
		        image = srcBi.getScaledInstance(level_width, level_height, Image.SCALE_SMOOTH);
		        tagBi = new BufferedImage(level_width, level_height, BufferedImage.TYPE_INT_RGB);
				g = tagBi.getGraphics();
				g.drawImage(image, 0, 0, null); // 绘制缩小后的图
				g.dispose();
	        	//this.cutImag(tagBi, descImgName + BaseDir.formartNumber(i), descType, tilesize, tilesize);
	        	//改用线程
				new CutImgThread(tagBi, descImgName + BaseDir.formartNumber(i), descType, tilesize, tilesize).start();
		        i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		 long start = System.currentTimeMillis();
		 ImageCut ic = new ImageCut();
		 ic.cutImagForPanojs(BaseDir.BASE_DIR+"datamap/main_component.png", BaseDir.BASE_DIR+"datamap/pyramid1/main_", "jpg", 256);
		 System.out.println("耗时:"+(System.currentTimeMillis()-start)/1000+"s");
	}
}

/**
 * 由于需要不同缩放的切割,用多线程去切割
 * 
 * @dp_new
 * @author zhangxy
 * @Jun 27, 2013
 * @
 */
class CutImgThread extends Thread{
	private BufferedImage srcBi;
	private String desImgName;
	private String desType;
	private int desWidth;
	private int desHeight;
	
	public CutImgThread(BufferedImage srcBi, String desImgName, String desType,
			int desWidth, int desHeight) {
		this.srcBi = srcBi;
		this.desImgName = desImgName;
		this.desType = desType;
		this.desWidth = desWidth;
		this.desHeight = desHeight;
	}

	@Override
	public void run() {
		try {
			int srcWidth = srcBi.getWidth(); // 源图宽度
			int srcHeight = srcBi.getHeight(); // 源图高度
			if (srcWidth > desWidth && srcHeight > desHeight) {
				Image image = srcBi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH);
				int cols = 0; // 切片横向数量
				int rows = 0; // 切片纵向数量
				int lastRW = srcWidth % desWidth;
				int lastRH = srcHeight % desHeight;
				// 计算切片的横向和纵向数量
				if (lastRW == 0) {
					cols = srcWidth / desWidth;
				} else {
					cols = (int) Math.floor(srcWidth / desWidth) + 1;
				}
				
				if (lastRH == 0) {
					rows = srcHeight / desHeight;
				} else {
					rows = (int) Math.floor(srcHeight / desHeight) + 1;
				}
				
				// 循环建立切片
				// 改进的想法:是否可用多线程加快切割速度
				BufferedImage tagBi = null;
				Graphics g = null;
				Image img;
				ImageFilter cropFilter; // 读取源图像
				for (int i = 0; i < rows; i++) {
					cropFilter = new CropImageFilter(0, i * desHeight, srcWidth, desHeight);
					img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
					tagBi = new BufferedImage(srcWidth, desHeight, BufferedImage.TYPE_INT_RGB);
					g = tagBi.getGraphics();
					g.drawImage(img, 0, 0, null); // 绘制缩小后的图
					g.dispose();
					//改用线程-每一行 起一个线程
					new RowCutImgThread(i, rows, cols, lastRW, lastRH, img, desImgName, desType, desWidth, desHeight).start();
					/*for (int j = 0; j < cols; j++) {
						// 四个参数分别为图像起点坐标和宽高
						cropFilter = new CropImageFilter(j * desWidth, i * desHeight, lastRW > 0 && cols - 1 == j ? lastRW : desWidth, lastRH > 0 && rows - 1 == i ? lastRH : desHeight);
						img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
						tagBi = new BufferedImage(lastRW > 0 && cols - 1 == j ? lastRW : desWidth, lastRH > 0 && rows - 1 == i ? lastRH : desHeight, BufferedImage.TYPE_INT_RGB);
						g = tagBi.getGraphics();
						g.drawImage(img, 0, 0, null); // 绘制缩小后的图
						g.dispose();
						// 输出为文件
						ImageIO.write(tagBi, desType, new File(desImgName + "_" + BaseDir.formartNumber(j) + "_" + BaseDir.formartNumber(i) + "."+desType));
					}*/
				}
			}else{
				ImageIO.write(srcBi, desType, new File(desImgName + "_000_000."+desType));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

//按行启动线程
class RowCutImgThread extends Thread{
	private int currRow;
	private int cols;
	private int rows;
	private int lastRW;
	private int lastRH;
	private Image image;
	private String desImgName;
	private String desType;
	private int desWidth;
	private int desHeight;

	public RowCutImgThread(int currRow, int rows, int cols, int lastRW,
			int lastRH, Image image, String desImgName, String desType,
			int desWidth, int desHeight) {
		this.currRow = currRow;
		this.rows = rows;
		this.cols = cols;
		this.lastRW = lastRW;
		this.lastRH = lastRH;
		this.image = image;
		this.desImgName = desImgName;
		this.desType = desType;
		this.desWidth = desWidth;
		this.desHeight = desHeight;
	}

	@Override
	public void run() {
		try{
			BufferedImage tagBi = null;
			Graphics g = null;
			Image img = null;
			ImageFilter cropFilter = null; // 读取源图像
			for (int j = 0; j < cols; j++) {
				// 四个参数分别为图像起点坐标和宽高
				cropFilter = new CropImageFilter(j * desWidth, 0, lastRW > 0 && cols - 1 == j ? lastRW : desWidth, lastRH > 0 && rows - 1 == currRow ? lastRH : desHeight);
				img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
				tagBi = new BufferedImage(lastRW > 0 && cols - 1 == j ? lastRW : desWidth, lastRH > 0 && rows - 1 == currRow ? lastRH : desHeight, BufferedImage.TYPE_INT_RGB);
				g = tagBi.getGraphics();
				g.drawImage(img, 0, 0, null); // 绘制缩小后的图
				g.dispose();
				// 输出为文件
				ImageIO.write(tagBi, desType, new File(desImgName + "_" + BaseDir.formartNumber(j) + "_" + BaseDir.formartNumber(currRow) + "."+desType));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
发表评论
用户名: 匿名