Java读取并修改图片的像素值

Posted xiaoranone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java读取并修改图片的像素值相关的知识,希望对你有一定的参考价值。

Java程序读取图片像素的值类,

package work;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Image 
    /**
     * 读取一张图片的RGB值
     * 
     * @throws Exception
     */
    public void getImagePixel(String image) throws Exception 
        int[] rgb = new int[3];
        File file = new File(image);
        BufferedImage bi = null;
        try 
            bi = ImageIO.read(file);
         catch (Exception e) 
            e.printStackTrace();
        
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        System.out.println("width=" + width + ",height=" + height + ".");
        System.out.println("minx=" + minx + ",miniy=" + miny + ".");
        for (int i = minx; i < width; i++) 
            for (int j = miny; j < height; j++) 
                int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
                System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","
                        + rgb[1] + "," + rgb[2] + ")");
                bi.setRGB(123, 123, 123);
            
        
    

    /**
     * 返回屏幕色彩值
     * 
     * @param x
     * @param y
     * @return
     * @throws AWTException
     */
    public int getScreenPixel(int x, int y) throws AWTException  // 函数返回值为颜色的RGB值。
        Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
        rb = new Robot();
        Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
        Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
        System.out.println(di.width);
        System.out.println(di.height);
        Rectangle rec = new Rectangle(0, 0, di.width, di.height);
        BufferedImage bi = rb.createScreenCapture(rec);
        int pixelColor = bi.getRGB(x, y);

        return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
    

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception 
        int x = 0;
        Image rc = new Image();
        x = rc.getScreenPixel(100, 345);
        System.out.println(x + " - ");
        rc.getImagePixel("D:\\\\xiao.jpg");
    


Java读取并修改像素的值。

package work;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Image1 

     private void setAlpha(String os) 
            /**
             * 增加测试项
             * 读取图片,绘制成半透明,修改像素
             */
            try 
              ImageIcon imageIcon = new ImageIcon(os);
              BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(),imageIcon.getIconHeight()
                  , BufferedImage.TYPE_4BYTE_ABGR);
              Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
              g2D.drawImage(imageIcon.getImage(), 0, 0,
                                   imageIcon.getImageObserver());
              //循环每一个像素点,改变像素点的Alpha值
              int alpha = 100;
              for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) 
                for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) 
                  int pixel = bufferedImage.getRGB(j2, j1);
                  int[]   rgb = new int[3];
                  rgb[0] = (pixel & 0xff0000) >> 16;
                  rgb[1] = (pixel & 0xff00) >> 8;
                  rgb[2] = (pixel & 0xff);
                  System.out.println("i=" + j1 + ",j=" + j2 + ":(" + rgb[0] + ","
                            + rgb[1] + "," + rgb[2] + ")");

                  pixel = ( (alpha + 1) << 24) | (pixel & 0x00ffffff);
                  bufferedImage.setRGB(j2, j1, pixel);
                
              
              g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());

              //生成图片为PNG
              ImageIO.write(bufferedImage, "jpg",  new File("D:\\\\xiao.jpg"));
            
            catch (Exception e) 
              e.printStackTrace();
            
          
     public static void main(String[] args) throws Exception 
        int x = 0;
        Image1 rc = new Image1();
        rc.setAlpha("D:\\\\xiao.jpg");
        rc.setAlpha("D:\\\\xiao.jpg");
    


以上是关于Java读取并修改图片的像素值的主要内容,如果未能解决你的问题,请参考以下文章

Opencv读取图片像素值并保存为txt文件

如何获取PNG图片某点像素的rgba值

为啥用Python的openCV读取图片与PIL读取的图片像素值会不一样?

请问delphi 7 如何读取JPG图片的像素颜色值?

如何实现图片与像素值矩阵(或坐标)的互换

openmv读取像素慢如何解决