如何删除除特定rgb值以外的所有颜色 [英] How to remove all color except a certain rgb value

查看:254
本文介绍了如何删除除特定rgb值以外的所有颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我需要让程序扫描屏幕上的某个rgb值,然后将其更改为亮粉红色。我编写了程序,到目前为止,它会扫描屏幕上的某种颜色。我不知道如何改变像素颜色。我想我需要上传图片而不是扫描屏幕。但是,如果这样做,我仍然不知道如何更改像素。

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();
    }
}


推荐答案

有看看这些帖子。您应该使用 ColorModel ,这是进行像素颜色替换的最快方法,而不是遍历像素:

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

这篇关于如何删除除特定rgb值以外的所有颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆