Java development _ image capture tool implementation principle

  • 2020-04-01 01:08:28
  • OfStack

Here's how it works:

Test a:

The original:

< img Alt = "" border = 0 height = 480 SRC =" / / files.jb51.net/file_images/article/201211/2012112711314840.png "width = 483 >

Effect:

< img Alt = "" border = 0 height = 366 SRC =" / / files.jb51.net/file_images/article/201211/2012112711314841.png "width = 366 >

Test 2:

The original:

< img Alt = "" border = 0 height = 370 SRC =" / / files.jb51.net/file_images/article/201211/2012112711314842.jpg "width = 500 >

Effect:

< img Alt = "" border = 0 height = 366 SRC =" / / files.jb51.net/file_images/article/201211/2012112711314843.jpg "width = 366 >

Code part:

 
 
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; 
 
public class ImageCut { 
 
private String srcpath = "e:/poool.jpg"; 
 
private String subpath = "e:/pool_end"; 
 
private static final String IMAGE_FORM_OF_JPG = "jpg"; 
 
private static final String IMAGE_FORM_OF_PNG = "png"; 
 
private int x; 
 
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()); 
} 
 
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); 
} 
} 
 
public void cut(String srcpath, String subpath) throws IOException { 
FileInputStream is = null; 
ImageInputStream iis = null; 
try { 
//Read image file
is = new FileInputStream(srcpath); 
//Gets the suffix name of the file
String postFix = getPostfix(srcpath); 
System.out.println(" The picture format is: " + postFix); 
 
Iterator<ImageReader> it = getImageReadersByFormatName(postFix); 
ImageReader reader = it.next(); 
//Get picture stream
iis = ImageIO.createImageInputStream(is); 
/* 
* <p>iis: Read the source .true: Only search forward  </p>. I'm going to label it as   'only search forward ' .  
*  This setting means that the images contained in the input source will be read only sequentially, possibly allowing  reader  Avoid caching those parts of the input that contain data associated with previously read images.  
*/ 
reader.setInput(iis, true); 
/* 
* <p> Class that describes how streams are decoded <p>. Used to specify how to enter from  Java Image I/O 
*  A stream in the context of a frame converts an image or group of images. A plug-in for a particular image format   From its  ImageReader  Implementation of the  
* getDefaultReadParam  Return in method  ImageReadParam  The instance.  
*/ 
ImageReadParam param = reader.getDefaultReadParam(); 
 
Rectangle rect = new Rectangle(x, y, width, height); 
//Provides a BufferedImage to be used as the target for decoding pixel data.
param.setSourceRegion(rect); 
 
BufferedImage bi = reader.read(0, param); 
//Save new pictures
ImageIO.write(bi, postFix, new File(subpath + "_" + new Date().getTime() + "." + postFix)); 
} finally { 
if (is != null) 
is.close(); 
if (iis != null) 
iis.close(); 
} 
} 
/** 
*  To obtain inputFilePath Suffixed names such as: "e:/test.pptx" Suffix: "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; 
} 
} 


Related articles: