package com.ssy.cutimg.tools.test;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
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 java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* 将图片切成四份
* 切10000像素以上的 同时切两张 不能同时三张
* */
public class ThreadCutLargeImg2 {
static int threadNumber = 8;
static final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
public static void main(String[] args) throws IOException {
System.out.println("Hello World");
long startTime = System.currentTimeMillis();// 开始时间
Date date = new Date();
String strDateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println("----------------------------开始时间----------------------------:"+sdf.format(date));
ThreadCutLargeTiles my = new ThreadCutLargeTiles();
my.init();
for (int i = 0; i < threadNumber; i++) {
new Thread(my, "thread" + i).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
int mill = (int) ((System.currentTimeMillis() - startTime) / 1000);
date = new Date();
sdf = new SimpleDateFormat(strDateFormat);
System.out.println("----------------------------结 束 时 间----------------------------:"+sdf.format(date));
System.out.println("----------------------------执行总时间----------------------------:" + (mill / 60) + " 分 " + (mill % 60) + " 秒 ");// 72388
}
}
/**
* @author ssy
*/
class ThreadCutLargeTiles implements Runnable {
LinkedList<File> files = new LinkedList<File>();
public static String defPath = "c://javaImg//";
public void init() throws IOException {
File defFile = new File(defPath);
if (defFile.isDirectory()) {
File[] listFiles = defFile.listFiles();
for (File f : listFiles) {
String weizhui = f.getName().substring(f.getName().lastIndexOf('.')+1);
if (f.isFile()) {
if("jpg".equals(weizhui)||"png".equals(weizhui)||"JPG".equals(weizhui)||"PNG".equals(weizhui)){
files.addLast(f);
}
}
}
} else {
System.out.println("++目录不存在" + defPath);
}
String succDirStr = defPath + "success//ori//";
File succFile = new File(succDirStr);
if (!succFile.exists()) {
succFile.mkdirs();
}
}
@Override
public void run() {
System.out.println("=运行线程 " + Thread.currentThread().getName());
while (!files.isEmpty()) {
File origionFile = files.removeLast();
System.out.println(origionFile.getName());
if (origionFile.isFile()) {
largeCut(origionFile);
}
}
ThreadCutLargeImg2.countDownLatch.countDown();
}
/*
* image 转 BufferedImage
* */
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage)image;
}
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(
image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
private static void largeCut(File origionFile) {
int rows = 2;
int cols = 2;
String descDir = defPath + "largeCut//" ;
File descFile = new File(descDir);
descFile.mkdirs();
try {
System.out.println("--------------操作瓦片--------------"+origionFile);
BufferedImage bi = ImageIO.read(new File(origionFile.toString()));
int srcWidth = bi.getWidth();
int srcHeight = bi.getHeight();
if (srcWidth > 0 && srcHeight > 0) {
Image img;
ImageFilter cropFilter;
Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
int destWidth = srcWidth;
int destHeight = srcHeight;
if (srcWidth % rows == 0) {
destWidth = srcWidth / rows;
} else {
destWidth = (int) Math.floor(srcWidth / rows) + 1;
}
if (srcHeight % cols == 0) {
destHeight = srcHeight / cols;
} else {
destHeight = (int) Math.floor(srcWidth / cols) + 1;
}
for (int j = 1; j < cols + 1; j++) {
for (int i = 1; i < rows + 1; i++) {
File tileFile = new File(descDir);
cropFilter = new CropImageFilter((i - 1) * destWidth, (j - 1) * destHeight, destWidth,destHeight);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
/*------------------TYPE_INT_ARGB是png图片----------------TYPE_INT_RGB是jpg图片---还有许多类型 灰度之类的-------*/
BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_ARGB);
// BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = tag.createGraphics();// new
g.drawImage(img, 0, 0, null);
g.dispose();
String javaimgname = "javaimgname";
if(tileFile.exists()){
String newFile = tileFile.toString();
ImageIO.write(tag, "PNG", new File(newFile + "/"+javaimgname+j+""+i+".png"));
}else{
tileFile.mkdirs();
String newFile = tileFile.toString();
ImageIO.write(tag, "PNG", new File(newFile + "/"+javaimgname+j+""+i+".png"));
}
}
}
}
if (origionFile.renameTo(new File(defPath + "success//oper//" + origionFile.getName()))) {
System.out.println("+++++++++++++++++++++++++文件移动成功+++++++++++++++++++++++++" + defPath + origionFile.getName());
} else {
System.out.println("-------------------------文件移动失败-------------------------" + defPath + origionFile.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
切PNG图片和JPG图片需用不同方法,PNG用TYPE_INT_ARGB,JPG用TYPE_INT_RGB
问题
改成网页版部署在tomcat下,执行后内存升高,执行结束后也未降低,是不是线程的问题,或者执行结束或应清理下内存,应该怎么设置TAT
版权声明:本文为qq_35505699原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。