国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java開發(fā)_圖片截取工具實(shí)現(xiàn)原理

2019-11-26 16:17:38
字體:
供稿:網(wǎng)友

先來看看效果:

測試一:

原圖:

效果圖:

測試二:

原圖:

效果圖:

代碼部分:

復(fù)制代碼 代碼如下:

/**
*
*/
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";
/**
* 剪切點(diǎn)x坐標(biāo)
*/
private int x;
/**
* 剪切點(diǎn)y坐標(biāo)
*/
private int y;
/**
* 剪切點(diǎn)寬度
*/
private int width;
/**
* 剪切點(diǎn)高度
*/
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());
}
/**
* 返回包含所有當(dāng)前已注冊 ImageReader 的 Iterator,這些 ImageReader 聲稱能夠解碼指定格式。
* 參數(shù):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);
/*
* 返回包含所有當(dāng)前已注冊 ImageReader 的 Iterator,這些 ImageReader 聲稱能夠解碼指定格式。
* 參數(shù):formatName - 包含非正式格式名稱 .(例如 "jpeg" 或 "tiff")等 。
*/
Iterator<ImageReader> it = getImageReadersByFormatName(postFix);
ImageReader reader = it.next();
// 獲取圖片流
iis = ImageIO.createImageInputStream(is);
/*
* <p>iis:讀取源.true:只向前搜索 </p>.將它標(biāo)記為 ‘只向前搜索'。
* 此設(shè)置意味著包含在輸入源中的圖像將只按順序讀取,可能允許 reader 避免緩存包含與以前已經(jīng)讀取的圖像關(guān)聯(lián)的數(shù)據(jù)的那些輸入部分。
*/
reader.setInput(iis, true);
/*
* <p>描述如何對流進(jìn)行解碼的類<p>.用于指定如何在輸入時(shí)從 Java Image I/O
* 框架的上下文中的流轉(zhuǎn)換一幅圖像或一組圖像。用于特定圖像格式的插件 將從其 ImageReader 實(shí)現(xiàn)的
* getDefaultReadParam 方法中返回 ImageReadParam 的實(shí)例。
*/
ImageReadParam param = reader.getDefaultReadParam();
/*
* 圖片裁剪區(qū)域。Rectangle 指定了坐標(biāo)空間中的一個(gè)區(qū)域,通過 Rectangle 對象
* 的左上頂點(diǎn)的坐標(biāo)(x,y)、寬度和高度可以定義這個(gè)區(qū)域。
*/
Rectangle rect = new Rectangle(x, y, width, height);
// 提供一個(gè) BufferedImage,將其用作解碼像素?cái)?shù)據(jù)的目標(biāo)。
param.setSourceRegion(rect);
/*
* 使用所提供的 ImageReadParam 讀取通過索引 imageIndex 指定的對象,并將 它作為一個(gè)完整的
* 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;
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 小金县| 龙山县| 安龙县| 特克斯县| 石台县| 淮北市| 新宾| 霍林郭勒市| 临城县| 清丰县| 石狮市| 革吉县| 辛集市| 青冈县| 桦川县| 梁山县| 布尔津县| 南涧| 白玉县| 车险| 温泉县| 平凉市| 喜德县| 岫岩| 浦城县| 兴义市| 元朗区| 新乡县| 延川县| 元江| 合水县| 乡城县| 长沙县| 申扎县| 香港 | 卓尼县| 钦州市| 张家口市| 霞浦县| 湘潭县| 咸阳市|