java多线程分隔图片程序

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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值