TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
java象素级处理是最近自己在玩的东西,下面这个程序就能获得图片任意一点的象素值。 用java分离象素, 只是java象素级处理的小技巧。颜色分离完毕就可以对单一的色彩进行数学处理。 这个程序主要用到awt的两个类: 1.PixelGrabber :将图片分解成象素存入int[]数组。alpha,green,red ,blue各占8位(bit) 24-31 位表示 alpha 分量,16-23 位表示红色分量,8-15 位表示绿色分量,0-7 位表示蓝色分量。 可以用这个类打开java图像数字信号处理的大门^_^ 2.MemoryImageSource :将int[]数组合成图片.功能和PixelGrabber相反
ImagemodImg = JFrame.createImage(
new MemoryImageSource(w,h,colors,0,w))
注释一下: 电脑的颜色合成效果和光的原理相同。蓝,红,绿相加等于白色,而不是黑色。
最深的颜色是oxff,黑色是0,单种颜色无法表示白色。
 

- /*
- *author by liuke
- *email: soulnew@gmail.com
- *blog: souljava.blog.163.com
- **/
- import java.awt.image.PixelGrabber;
- import java.awt.Image;
- import java.awt.image.ImageObserver;
- import javax.swing.ImageIcon;
- import java.io.FileWriter;
- import javax.swing.JFrame;
- import java.awt.image.MemoryImageSource;
- import java.awt.Graphics2D;
- import java.io.File;
- public class ByPixelGrabber{
- FileWriter fw;
- double index=0.0;
- int[] blues,reds,greens;
- int[] temp;
- int colorindex=0;
- int w,h;
- String imagename="img/try.jpg";
-
- public ByPixelGrabber(){
- ImageIcon image=new ImageIcon(imagename);
- w=image.getIconWidth();
- h=image.getIconHeight();
- handlepixels(image.getImage(),0,0,w,h);
- saveImage(createImage(blues),"blues.jpg");
- saveImage(createImage(greens),"greens.jpg");
- saveImage(createImage(reds),"reds.jpg");
- }
-
- public void handlesinglepixel(int x, int y, int pixel) {
-
-
- int alpha = (pixel >> 24) & 0xff;
- int red = (pixel >> 16) & 0xff;
- int green = (pixel >> 8) & 0xff;
- int blue = (pixel ) & 0xff;
-
- alpha=alpha<<24;
- red=red<<16;
- green=green<<8;
-
- blue=blue|alpha;
- green=green|alpha;
- red=red|alpha;
-
- blues[colorindex]=blue;
- reds[colorindex]=red;
- greens[colorindex]=green;
- colorindex++;
- }
-
- public Image createImage(int[] colors){
- JFrame jf=new JFrame();
- Image modImg = jf.createImage(
- new MemoryImageSource(
- w,h,colors,0,w));
- return modImg;
- }
-
- public void saveImage(Image image,String filename){
- java.awt.image.BufferedImage bi = new java.awt.image.BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_BGR);
- Graphics2D g2= (Graphics2D)bi.getGraphics();
- g2.drawImage(image,0,0,null);
-
- try{
- javax.imageio.ImageIO.write(bi, "JPEG", new java.io.File(filename));
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- public void handlepixels(Image img, int x, int y, int w, int h) {
- int[] pixels = new int[w * h];
- blues=new int[w * h];
- reds=new int[w * h];
- greens=new int[w * h];
-
- PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
- temp=pixels;
- try {
- pg.grabPixels();
- } catch (InterruptedException e) {
- System.err.println("interrupted waiting for pixels!");
- return;
- }
- if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
- System.err.println("image fetch aborted or errored");
- return;
- }
- for (int j = 0; j < h; j++) {
- for (int i = 0; i < w; i++) {
- handlesinglepixel(x+i, y+j, pixels[j * w + i]);
- }
- }
- }
-
- public static void main(String[] args){
- ByPixelGrabber pg=new ByPixelGrabber();
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/10/30/235703281.zip |
|