자바 개발사진 캡 처 도구 실현 원리

먼저 효과 보기:
테스트 1:
원본 그림:

효과 그림:

테스트 2:
원본 그림:

효과 그림:

코드 부분:
 
/**
*
*/
package com.b510;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
/**
* @date 2012-11-26
* @author xhw
*
*/
public class ImageCut {
/**
* :c:\1.jpg
*/
private String srcpath = "e:/poool.jpg";
/**
* . :c:\2.jpg
*/
private String subpath = "e:/pool_end";
/**
* jpg
*/
private static final String IMAGE_FORM_OF_JPG = "jpg";
/**
* png
*/
private static final String IMAGE_FORM_OF_PNG = "png";
/**
* x
*/
private int x;
/**
* y
*/
private int y;
/**
*
*/
private int width;
/**
*
*/
private int height;
public ImageCut() {
}
public ImageCut(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public static void main(String[] args) throws Exception {
ImageCut imageCut = new ImageCut(134, 0, 366, 366);
imageCut.cut(imageCut.getSrcpath(), imageCut.getSubpath());
}
/**
* ImageReader Iterator, ImageReader 。
* :formatName - .( "jpeg" "tiff") 。
*
* @param postFix
*
* @return
*/
public Iterator<ImageReader> getImageReadersByFormatName(String postFix) {
switch (postFix) {
case IMAGE_FORM_OF_JPG:
return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_JPG);
case IMAGE_FORM_OF_PNG:
return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_PNG);
default:
return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_JPG);
}
}
/**
* , 。
* @param srcpath
* @param subpath
* @throws IOException
*/
public void cut(String srcpath, String subpath) throws IOException {
FileInputStream is = null;
ImageInputStream iis = null;
try {
//
is = new FileInputStream(srcpath);
//
String postFix = getPostfix(srcpath);
System.out.println(" :" + postFix);
/*
* ImageReader Iterator, ImageReader 。
* :formatName - .( "jpeg" "tiff") 。
*/
Iterator<ImageReader> it = getImageReadersByFormatName(postFix);
ImageReader reader = it.next();
//
iis = ImageIO.createImageInputStream(is);
/*
* <p>iis: .true: </p>. ‘ '。
* , reader 。
*/
reader.setInput(iis, true);
/*
* <p> <p>. Java Image I/O
* 。 ImageReader
* getDefaultReadParam ImageReadParam 。
*/
ImageReadParam param = reader.getDefaultReadParam();
/*
* 。Rectangle , Rectangle
* (x,y)、 。
*/
Rectangle rect = new Rectangle(x, y, width, height);
// BufferedImage, 。
param.setSourceRegion(rect);
/*
* ImageReadParam imageIndex ,
* BufferedImage 。
*/
BufferedImage bi = reader.read(0, param);
//
ImageIO.write(bi, postFix, new File(subpath + "_" + new Date().getTime() + "." + postFix));
} finally {
if (is != null)
is.close();
if (iis != null)
iis.close();
}
}
/**
* inputFilePath , :"e:/test.pptx" :"pptx"<br>
*
* @param inputFilePath
* @return
*/
public String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getSrcpath() {
return srcpath;
}
public void setSrcpath(String srcpath) {
this.srcpath = srcpath;
}
public String getSubpath() {
return subpath;
}
public void setSubpath(String subpath) {
this.subpath = subpath;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

좋은 웹페이지 즐겨찾기