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报春到你的电脑上了。下面程序的运行效果吧!