java開發(fā)_圖片截取工具實(shí)現(xiàn)原理
先來看看效果:
測試一:
原圖:
效果圖:
測試二:
原圖:
效果圖:
代碼部分:
/**
*
*/
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)前已注冊(cè) 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);
}
}
/**
* 對(duì)圖片裁剪,并把裁剪完蛋新圖片保存 。
* @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)前已注冊(cè) 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>描述如何對(duì)流進(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 對(duì)象
* 的左上頂點(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 指定的對(duì)象,并將 它作為一個(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;
}
}
- Java開發(fā)者必備10大數(shù)據(jù)工具和框架
- Java程序員新手老手常用的八大開發(fā)工具
- Java微信公眾平臺(tái)開發(fā)(14) 微信web開發(fā)者工具使用
- Java開發(fā)學(xué)習(xí) Java數(shù)組操作工具
- 值得Java開發(fā)者關(guān)注的7款新工具
- 使用Eclipse開發(fā)工具如何解決Java Compiler中Annotation Processin不出現(xiàn)的問題
- 分享15款Java程序員必備的開發(fā)工具
- 初學(xué)java常用開發(fā)工具介紹
- [JAVA]十四種Java開發(fā)工具點(diǎn)評(píng)
- Java開發(fā)者推薦的10種常用工具
相關(guān)文章
SpringBoot訪問接口自動(dòng)跳轉(zhuǎn)login頁面的問題及解決
這篇文章主要介紹了SpringBoot訪問接口自動(dòng)跳轉(zhuǎn)login頁面的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot JPA實(shí)現(xiàn)查詢多值
這篇文章主要為大家詳細(xì)介紹了SpringBoot JPA實(shí)現(xiàn)查詢多值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08elasticsearch+logstash并使用java代碼實(shí)現(xiàn)日志檢索
這篇文章主要介紹了elasticsearch+logstash并使用java代碼實(shí)現(xiàn)日志檢索,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02SpringBoot項(xiàng)目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決
這篇文章主要介紹了SpringBoot項(xiàng)目網(wǎng)頁加載出現(xiàn)Whitelabel?Error?Page的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11解決idea使用maven編譯正常但是運(yùn)行項(xiàng)目時(shí)卻提示很多jar包找不到的問題
這篇文章主要介紹了解決idea使用maven編譯正常但是運(yùn)行項(xiàng)目時(shí)卻提示很多jar包找不到的問題,本文分多種情形給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07java計(jì)算方差、標(biāo)準(zhǔn)差(均方差)實(shí)例代碼
在本篇文章里小編給大家分享了關(guān)于java計(jì)算方差、標(biāo)準(zhǔn)差(均方差)實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考下。2019-08-08