(JAVA)获取图片轮廓
2015-11-05 12:58
309 查看
原始图片类型为 jpg(type:TYPE_3BYTE_BGR)
1.读取图片
2.获取图片像素值
3.创建新图片
4.设置图片像素值
5.保存新图片
整体程序
参考
BufferedImage类
http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/awt/image/BufferedImage.html#getRGB%28int,%20int%29
1.读取图片
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
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
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统