(JAVA)获取图片轮廓

来源:互联网 发布:小米平板2网络模块坏 编辑:程序博客网 时间:2024/05/23 17:46

原始图片类型为 jpg(type:TYPE_3BYTE_BGR)

1.读取图片

File filepath = new File("e://shang.jpg");   //image file pathBufferedImage image = ImageIO.read(filepath);    //image's read_bufferint type= image.getType();    //image's type  TYPE_3BYTE_BGR = 5

2.获取图片像素值

image.getRGB(x, y)//point (x,y) pixel

3.创建新图片

BufferedImage outImg = null;outImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);//TYPE_INT_ARGB是color model

4.设置图片像素值

outImg.setRGB(x, y, rgb);

5.保存新图片

File outFile = new File("e://generate.png");  try {         ImageIO.write(outImg, "png", outFile);      } catch (IOException e) {         e.printStackTrace();      }  

整体程序




import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.awt.Rectangle;import java.awt.image.Raster;import javax.imageio.ImageIO;public class getImg {    /**     * @param args     * @throws IOException      */    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub         File filepath = new File("e://shang.jpg");   //image file path         BufferedImage image = ImageIO.read(filepath);    //image's read_buffer         int type= image.getType();    //image's type  TYPE_3BYTE_BGR = 5         Raster ra = image.getData();         Rectangle rect = ra.getBounds();         int nImageDate[] = new int[rect.height*rect.width];         int nTemp[] = new int[rect.width*rect.height];         //nImageDate = ra.getPixels(0,0,rect.width, rect.height,nTemp);          //nImageDate = image.getRGB(0, 0, image.getWidth(), image.getHeight(), nTemp, 0, 0);         System.out.println( "type: "+type );         System.out.println( "image.width: "+ image.getWidth() );         System.out.println( "image.height: "+ image.getHeight() );         BufferedImage outImg = null;         outImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);         int x,y;         int rgb;         for( y=0;y<259;y++)  //按行扫描         {             for( x=0;x<282;x++)             {                 if( x==0 || y==0 || x==281 || y==258 ) continue;                 rgb = image.getRGB(x, y);                 if( image.getRGB(x, y)!=-1 && (image.getRGB(x-1, y)==-1||image.getRGB(x+1, y)==-1) ){ //判断该点是不是轮廓点                     outImg.setRGB(x, y, rgb);                     System.out.println(rgb);                 }             }         }         for( x=0;x<282;x++) //按列扫描         {             for( y=0;y<259;y++)              {                 if( x==0 || y==0 || x==281 || y==258 ) continue;                 rgb = image.getRGB(x, y);                 if( image.getRGB(x, y)!=-1 && (image.getRGB(x, y+1)==-1||image.getRGB(x, y-1)==-1) ){ //判断该点是不是轮廓点                     outImg.setRGB(x, y, rgb);                 }             }         }         //outImg.setRGB( 0, 0, image.getWidth(), image.getHeight(), nTemp, 0 , image.getWidth() );            File outFile = new File("e://generate.png");           try {               ImageIO.write(outImg, "png", outFile);           } catch (IOException e) {               e.printStackTrace();           }      }}

参考
BufferedImage类
http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/awt/image/BufferedImage.html#getRGB%28int,%20int%29

0 0
原创粉丝点击