BufferedImage類有一個getSubimage()方法,以下來自API

滄源網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),滄源網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為滄源上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的滄源做網(wǎng)站的公司定做!
public BufferedImage getSubimage(int x,
int y,
int w,
int h)
返回由指定矩形區(qū)域定義的子圖像。返回的 BufferedImage 與源圖像共享相同的數(shù)據(jù)數(shù)組。
參數(shù):
x - 指定矩形區(qū)域左上角的 X 坐標(biāo)
y - 指定矩形區(qū)域左上角的 Y 坐標(biāo)
w - 指定矩形區(qū)域的寬度
h - 指定矩形區(qū)域的高度
返回:
BufferedImage,它是此 BufferedImage 的子圖像。
拋出:
RasterFormatException - 如果指定區(qū)域不包含在此 BufferedImage 中。
package?test;
import?java.awt.Color;
import?java.awt.Graphics2D;
import?java.awt.Image;
import?java.awt.geom.AffineTransform;
import?java.awt.image.AffineTransformOp;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.IOException;
import?java.nio.Buffer;
import?javax.imageio.ImageIO;
import?javax.imageio.stream.ImageOutputStream;
/**
*?裁剪、縮放圖片工具類
*?
*?@author?CSDN?沒有夢想-何必遠(yuǎn)方
*/
public?class?ImgUtils?{
/**
?*?縮放圖片方法
?*?
?*?@param?srcImageFile
?*????????????要縮放的圖片路徑
?*?@param?result
?*????????????縮放后的圖片路徑
?*?@param?height
?*????????????目標(biāo)高度像素
?*?@param?width
?*????????????目標(biāo)寬度像素
?*?@param?bb
?*????????????是否補白
?*/
public?final?static?void?scale(String?srcImageFile,?String?result,
int?height,?int?width,?boolean?bb)?{
try?{
double?ratio?=?0.0;?//?縮放比例
File?f?=?new?File(srcImageFile);
BufferedImage?bi?=?ImageIO.read(f);
Image?itemp?=?bi.getScaledInstance(width,?height,?bi.SCALE_SMOOTH);//?bi.SCALE_SMOOTH
//?選擇圖像平滑度比縮放速度具有更高優(yōu)先級的圖像縮放算法。
//?計算比例
if?((bi.getHeight()??height)?||?(bi.getWidth()??width))?{
double?ratioHeight?=?(new?Integer(height)).doubleValue()
/?bi.getHeight();
double?ratioWhidth?=?(new?Integer(width)).doubleValue()
/?bi.getWidth();
if?(ratioHeight??ratioWhidth)?{
ratio?=?ratioHeight;
}?else?{
ratio?=?ratioWhidth;
}
AffineTransformOp?op?=?new?AffineTransformOp(AffineTransform//?仿射轉(zhuǎn)換
.getScaleInstance(ratio,?ratio),?null);//?返回表示剪切變換的變換
itemp?=?op.filter(bi,?null);//?轉(zhuǎn)換源?BufferedImage?并將結(jié)果存儲在目標(biāo)
//?BufferedImage?中。
}
if?(bb)?{//?補白
BufferedImage?image?=?new?BufferedImage(width,?height,
BufferedImage.TYPE_INT_RGB);//?構(gòu)造一個類型為預(yù)定義圖像類型之一的
//?BufferedImage。
Graphics2D?g?=?image.createGraphics();//?創(chuàng)建一個
//?Graphics2D,可以將它繪制到此
//?BufferedImage?中。
g.setColor(Color.white);//?控制顏色
g.fillRect(0,?0,?width,?height);//?使用?Graphics2D?上下文的設(shè)置,填充?Shape
//?的內(nèi)部區(qū)域。
if?(width?==?itemp.getWidth(null))
g.drawImage(itemp,?0,?(height?-?itemp.getHeight(null))?/?2,
itemp.getWidth(null),?itemp.getHeight(null),
Color.white,?null);
else
g.drawImage(itemp,?(width?-?itemp.getWidth(null))?/?2,?0,
itemp.getWidth(null),?itemp.getHeight(null),
Color.white,?null);
g.dispose();
itemp?=?image;
}
ImageIO.write((BufferedImage)?itemp,?"JPEG",?new?File(result));?//?輸出壓縮圖片
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
/**
?*?裁剪圖片方法
?*?
?*?@param?bufferedImage
?*????????????圖像源
?*?@param?startX
?*????????????裁剪開始x坐標(biāo)
?*?@param?startY
?*????????????裁剪開始y坐標(biāo)
?*?@param?endX
?*????????????裁剪結(jié)束x坐標(biāo)
?*?@param?endY
?*????????????裁剪結(jié)束y坐標(biāo)
?*?@return
?*/
public?static?BufferedImage?cropImage(BufferedImage?bufferedImage,
int?startX,?int?startY,?int?endX,?int?endY)?{
int?width?=?bufferedImage.getWidth();
int?height?=?bufferedImage.getHeight();
if?(startX?==?-1)?{
startX?=?0;
}
if?(startY?==?-1)?{
startY?=?0;
}
if?(endX?==?-1)?{
endX?=?width?-?1;
}
if?(endY?==?-1)?{
endY?=?height?-?1;
}
BufferedImage?result?=?new?BufferedImage(endX?-?startX,?endY?-?startY,
4);
for?(int?x?=?startX;?x??endX;?++x)?{
for?(int?y?=?startY;?y??endY;?++y)?{
int?rgb?=?bufferedImage.getRGB(x,?y);
result.setRGB(x?-?startX,?y?-?startY,?rgb);
}
}
return?result;
}
public?static?void?main(String[]?args)?throws?IOException?{
File?input?=?new?File("input.jpg");
BufferedImage?img?=?ImageIO.read(input);
cropImage(img,?10,?10,?20,?20);
File?output?=?new?File("output.jpg");
ImageIO.write(img,?"jpg",?output);
}
}
用map標(biāo)簽,在drw里用熱區(qū),拖動就可以了,你試試
代碼會自動生成,如下:
map
name="Map"
id="Map"area
shape="rect"
coords="104,303,223,357"
href=""
/
/map
你只要換掉104,303,223,357(圖片區(qū)域上下左右坐標(biāo)的位置)和超鏈接地址即可
總體思想
前臺網(wǎng)頁用js得到裁剪圖片的id及x y 寬度和高度
服務(wù)端根據(jù)id取出要裁剪的圖片
根據(jù)這些參數(shù)來生成裁剪的圖像 后臺代碼如下
java 代碼
package wodexiangce;
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:\ jpg
private String srcpath ;
// ===剪切圖片存放路徑名稱 如 c:\ jpg
private String subpath ;
// ===剪切點x坐標(biāo)
private int x ;
private int y ;
// ===剪切點寬度
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)
/*
* piis:讀取源 true:只向前搜索 /p 將它標(biāo)記為 只向前搜索
* 此設(shè)置意味著包含在輸入源中的圖像將只按順序讀取 可能允許 reader
* 避免緩存包含與以前已經(jīng)讀取的圖像關(guān)聯(lián)的數(shù)據(jù)的那些輸入部分
*/
reader setInput(iis true ) ;
/*
* p描述如何對流進行解碼的類p 用于指定如何在輸入時從 Java Image I/O
* 框架的上下文中的流轉(zhuǎn)換一幅圖像或一組圖像 用于特定圖像格式的插件
* 將從其 ImageReader 實現(xiàn)的 getDefaultReadParam 方法中返回
* ImageReadParam 的實例
*/
ImageReadParam param = reader getDefaultReadParam()
/*
* 圖片裁剪區(qū)域 Rectangle 指定了坐標(biāo)空間中的一個區(qū)域 通過 Rectangle 對象
* 的左上頂點的坐標(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( 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) throws Exception {
String name = d:\ jpg ;
OperateImage o = new OperateImage( )
o setSrcpath(name)
o setSubpath( D:\ jpg )
o cut() ;
}
lishixinzhi/Article/program/Java/hx/201311/26771
本文名稱:java程序裁剪圖片代碼 java程序裁剪圖片代碼是什么
當(dāng)前URL:http://chinadenli.net/article26/hpeocg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司、靜態(tài)網(wǎng)站、做網(wǎng)站、軟件開發(fā)、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)