1. java截取圖片
呵呵,很明確的告訴你:可以!
代碼半小時後出來!!!
……
終於出來了(呵呵,好像超過了半小時哈)且看代碼:
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.JApplet;
publicclassTestextendsJApplet{
Stringaddrs="F:\images\mm.bmp";//改成自己的圖片路徑
BufferedImagemm,child;
CutImageci;
publicTest(){
try{
mm=ImageIO.read(newFile(addrs));
}catch(IOExceptione){
System.out.println("圖片讀取失敗!");
e.printStackTrace();
}
ci=newCutImage(mm);
child=ci.getChildImage(50,0,150,220);
}
publicvoidinit(){
}
publicvoidpaint(Graphicsg){
g.setColor(Color.red);
g.drawString("原圖:",0,10);
g.drawImage(mm,20,10,this);
g.drawString("ci.getChildImage(50,0,150,220)截取後的圖片",mm.getWidth()+30,10);
g.drawImage(child,mm.getWidth()+50,20,this);
}
}
importjava.awt.Image;
importjava.awt.image.BufferedImage;
publicclassCutImage{
privateBufferedImageimg;
privateBufferedImagechild;
publicCutImage(){
}
publicCutImage(BufferedImageim){
img=im;
}
publicCutImage(Imageim){
img=(BufferedImage)im;
}
publicvoidsetImg(BufferedImageimg){
this.img=img;
}
(intx,inty,intwidth,intheight){
intcw=width;
intch=height;
intpw=img.getWidth();
intph=img.getHeight();
if(pw<x+width){
System.out.println("給出的參數超出原圖片的范圍!程序會自動減小寬度或高度");
cw=pw-x;
}
if(ph<y+height){
System.out.println("給出的參數超出原圖片的范圍!程序會自動減小寬度或高度");
ch=ph-y;
}
child=newBufferedImage(cw,ch,BufferedImage.TYPE_INT_ARGB);
for(inti=0;i<ch;i++){
for(intj=0;j<cw;j++){
child.setRGB(j,i,img.getRGB(x+j,y+i));
}
}
returnchild;
}
}
呵呵,希望樓主能夠滿意哦,如果你願意的話,稍微改一下代碼就可以把截取的圖片child報春到你的電腦上了。下面程序的運行效果吧!