How to remove all color except a certain rgb value

耗尽温柔 提交于 2019-12-13 04:55:56

问题


Okay I need to get the program to scan the screen for a certain rgb value then change it to bright pink. I made the program and so far it scans the screen for a certain color. I do not know how to get it to change the pixel color. I think I need to upload a picture instead of having it scan the screen. But if I do that I still wouldn't know how to change the pixels.

Here's my code so far.

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;


public class FindRgbOnScreen extends Thread{

    public Rectangle captureSize;

    public FindRgbOnScreen() {
        captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    }

    public void run() {
        try {
            Robot robot = new Robot();

            while(true){
                BufferedImage img = robot.createScreenCapture(captureSize);
                WritableRaster r = img.getRaster();
                DataBuffer db = r.getDataBuffer();
                DataBufferInt dbi = (DataBufferInt)db;
                int[] data = dbi.getData();                 

                for (int x_scale = 0; x_scale < captureSize.width; x_scale += 1) {  //this scans the screen 
                    for(int y_scale = 0; y_scale < captureSize.height; y_scale += 1) {
                        int rgb = data[x_scale + captureSize.width * y_scale];
                        if (rgb == -5381164){
                            //change pixel to pink
                        }
                        else{
                            //change pixel to grey
                        }
                    }
                }
            }
        }
        catch(AWTException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new FindRgbOnScreen().start();
    }
}

回答1:


Have a look at these posts. You should use ColorModel which is the fastest way to do pixel color replacement instead of iterating over pixels:

  • http://helpdesk.objects.com.au/java/changing-the-colormodel-of-a-bufferedimage
  • JAVA: Substitute one color with another using ColorModel
  • How to replace colors in BufferedImage in JAVA


来源:https://stackoverflow.com/questions/16877152/how-to-remove-all-color-except-a-certain-rgb-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!