java 根據(jù)坐標(biāo)截取圖片實(shí)例代碼
更新時間:2017年03月28日 15:00:33 投稿:lqh
這篇文章主要介紹了java 根據(jù)坐標(biāo)截取圖片實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
java 根據(jù)坐標(biāo)截取圖片
實(shí)例代碼:代碼中有不是注釋,很好看懂!
package com.json.test;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class OperateImage {
// ===源圖片路徑名稱如:c:\1.jpg
private String srcpath ;
// ===剪切圖片存放路徑名稱.如:c:\2.jpg
private String subpath ;
// ===剪切點(diǎn)x坐標(biāo)
private int x ;
private int y ;
// ===剪切點(diǎn)寬度
private int width ;
private int height ;
public OperateImage() {
}
public OperateImage( int x, int y, int width, int height) {
this .x = x ;
this .y = y ;
this .width = width ;
this .height = height ;
}
/**
* 對圖片裁剪,并把裁剪完蛋新圖片保存 。
*/
public void cut()throws IOException {
FileInputStream is = null ;
ImageInputStream iis = null ;
try {
// 讀取圖片文件
is =new FileInputStream(srcpath);
/*
* 返回包含所有當(dāng)前已注冊 ImageReader 的 Iterator,這些 ImageReader
* 聲稱能夠解碼指定格式。 參數(shù):formatName - 包含非正式格式名稱 .
*(例如 "jpeg" 或 "tiff")等 。
*/
Iterator < ImageReader > it=ImageIO.getImageReadersByFormatName("jpg");
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>.用于指定如何在輸入時從 Java Image I/O
* 框架的上下文中的流轉(zhuǎn)換一幅圖像或一組圖像。用于特定圖像格式的插件
* 將從其 ImageReader 實(shí)現(xiàn)的 getDefaultReadParam 方法中返回
* ImageReadParam 的實(shí)例。
*/
ImageReadParam param = reader.getDefaultReadParam();
/*
* 圖片裁剪區(qū)域。Rectangle 指定了坐標(biāo)空間中的一個區(qū)域,通過 Rectangle 對象
* 的左上頂點(diǎn)的坐標(biāo)(x,y)、寬度和高度可以定義這個區(qū)域。
*/
Rectangle rect = new Rectangle(x, y, width, height);
// 提供一個 BufferedImage,將其用作解碼像素數(shù)據(jù)的目標(biāo)。
param.setSourceRegion(rect);
/*
* 使用所提供的 ImageReadParam 讀取通過索引 imageIndex 指定的對象,并將
* 它作為一個完整的 BufferedImage 返回。
*/
BufferedImage bi=reader.read(0,param);
// 保存新圖片
ImageIO.write(bi,"jpg",new File(subpath));
} finally {
if (is != null )
is.close() ;
if (iis != null )
iis.close();
}
}
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;
}
public static void main(String[] args) {
OperateImage operateImage = new OperateImage(20, 20, 100, 100);
operateImage.srcpath = "C:/test/1.jpg";
operateImage.subpath = "C:/test/2.jpg";
try {
operateImage.cut();
} catch (IOException e) {
e.printStackTrace();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
metershpere實(shí)現(xiàn)調(diào)用自定義jar包中的方法
在MeterSphere接口測試中,面對多層循環(huán)邏輯和邏輯判斷等復(fù)雜情況,直接編寫測試用例往往顯得混亂不便,本文介紹了一個簡化這一過程的方法:首先使用IDEA創(chuàng)建Maven工程,編寫所需邏輯并生成jar包;然后在MeterSphere中上傳此jar包2024-10-10
SpringBoot中使用Quartz管理定時任務(wù)的方法
這篇文章主要介紹了SpringBoot中使用Quartz管理定時任務(wù)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Springboot定時任務(wù)Scheduled重復(fù)執(zhí)行操作
這篇文章主要介紹了Springboot定時任務(wù)Scheduled重復(fù)執(zhí)行操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java實(shí)現(xiàn)AES加密和解密方式完整示例
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)AES加密和解密方式的相關(guān)資料,AES加密為最常見的對稱加密算法,是一種區(qū)塊加密標(biāo)準(zhǔn),這個標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用,需要的朋友可以參考下2023-10-10

