Atitit.遍历图像像素点rgb java attilax总结
1. 遍历像素点
ImgxPicPhotoSplitor.java atibrow prj
publicstaticbooleancontainsWhiteLine(BufferedImageimage) {
intheit=image.getHeight();
for(inti=0;i
{
PixLinepl=getPixLine(image,i);
if(isWhiteLine(pl))
returntrue;
}
returnfalse;
}
2. 提取一行
这 个过程的下一步是用 Java 2D绘制图像。首先取得它的Graphics2D上下文。可以用方法createGraphics2D()或者调用getGraphics()做到这一点。在这个上下文上绘制将会自动修改图像的像素数据。在绘制完成后,可以用方法getRGB(int startX, int startY, int w, int h, int rgbArray, int offset, int scansize)容易且高效地提取图像的像素值。这个方法可以将图像中矩形区域的像素数据传输到一个整数数组中。getRGB()方法的参数如下:startX, startY 是要提取的区域左上角图像的坐标w, h 是要提取的区域的宽度和高度rgbArray 是接收像素值的整数数组offset 是数组中接收第一个像素值的位置的索引。
scansize 是图像中相邻两行中具有相同行索引的像素的索引偏移值。如果这个值与要提取的区域的宽度相同,那么一行的第一个像素就会存储在数组中前一行最后一个像素后 面的索引位置。如果这个值大于提取区域的宽度,那么数组中,在一行最后和下一行开始之间就会有一些未使用的索引。
走势这个getRGB好像有问题,不会调用,查找资料也不行。自豪嘎子写蓝。。
publicstaticPixLinegetPixLine(BufferedImageimage,intlineIndex) {
int[]pxs=newint[image.getWidth()];
for(inti=0;i
{
pxs[i]=image.getRGB(i,lineIndex);
}
PixLinepl=newPixLine();
pl.pxs=pxs;
returnpl;
}
作者:: 老哇的爪子 Attilax 艾龙, EMAIL:1466519819@qq.com
转载请注明来源: http://blog.csdn.net/attilax
3. Rgb分量提取
多谢sqcl的回答,还有一个问题,就是用BufferedImage.getRGB()返回的像素值是32位颜色值,要自己移位才能得到RBGA的各个分量值,有没有什么类可以配合BufferedImage直接取出某个像素的某个独立的分量值?
1
2
3
4
5
int pixel = 0xFF0000;
Color pixelColor = new Color(pixel);
int r = pixelColor.getRed();
int g = pixelColor.getGreen();
int b = pixelColor.getBlue();
不过,这样效率太低。用移位最好。如果觉得不方便,可以自己写个Helper类简单封装一下。
我们知道通过bufferedimage对象的getRGB(x,y)方法可以返回指定坐标的颜色int值 他可以通过
int R =(rgb & 0xff0000 ) >> 16 ;
int G= (rgb & 0xff00 ) >> 8 ;
int B= (rgb & 0xff );
转换成三个颜色分量
4. 其他读取像素
从BufferedImage对象中读取像素数据的代码如下:
1.inttype= image.getType();
2.if( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3.return(int[])image.getRaster().getDataElements(x, y, width, height, pixels );
4.else
5.returnimage.getRGB( x, y, width, height, pixels,0, width );
1.inttype= image.getType();
2.if( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3.return(int[])image.getRaster().getDataElements(x, y, width, height, pixels );
4.else
5.returnimage.getRGB( x, y, width, height, pixels,0, width );
首先获取图像类型,如果不是32位的INT型数据,直接读写RGB值即可,否则需要从Raster
对象中读取。
往BufferedImage对象中写入像素数据同样遵守上面的规则。代码如下:
1.inttype= image.getType();
2.if( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3.image.getRaster().setDataElements(x, y, width, height, pixels );
4.else
5.image.setRGB(x, y, width, height, pixels,0, width );
1.inttype= image.getType();
2.if( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
3.image.getRaster().setDataElements(x, y, width, height, pixels );
4.else
5.image.setRGB(x, y, width, height, pixels,0, width );
读取图像可能因为图像文件比较大,需要一定时间的等待才可以,Java Advance Image
Processor API提供了MediaTracker对象来跟踪图像的加载,同步其它操作,使用方法如下:
1.MediaTracker tracker =newMediaTracker(this);//初始化对象
2.tracker.addImage(image_01,1);// 加入要跟踪的BufferedImage对象image_001
3.tracker.waitForID(1,10000)// 等待10秒,让iamge_01图像加载
1.MediaTracker tracker =newMediaTracker(this);//初始化对象
2.tracker.addImage(image_01,1);// 加入要跟踪的BufferedImage对象image_001
3.tracker.waitForID(1,10000)// 等待10秒,让iamge_01图像加载
从一个32位int型数据cARGB中读取图像RGB颜色值的代码如下:
1.intalpha = (cARGB >>24)&0xff;//透明度通道
2.intred = (cARGB >>16) &0xff;
3.intgreen = (cARGB >>8) &0xff;
4.intblue = cARGB &0xff;
1.intalpha = (cARGB >>24)&0xff;//透明度通道
2.intred = (cARGB >>16) &0xff;
3.intgreen = (cARGB >>8) &0xff;
4.intblue = cARGB &0xff;
将RGB颜色值写入成一个INT型数据cRGB的代码如下:
1.cRGB = (alpha <<24) | (red<<16) | (green <<8) | blue;
1.cRGB = (alpha <<24) | (red<<16) | (green <<8) | blue;
创建一个BufferedImage对象的代码如下:
1.BufferedImage image = newBufferedImage(256,256, BufferedImage.TYPE_INT_ARGB);
1.BufferedImage image = newBufferedImage(256,256, BufferedImage.TYPE_INT_ARGB);
5. --code
ImgxPicPhotoSplitor.java atibrow prj
6. 参考
Java数字图像处理基础知识 - 必读 - 流浪的鱼 - 博客频道 - CSDN.NET.htm
Java数字图像处理基础知识-必读-流浪的鱼-博客频道- CSDN.NET.html