0

嘿,我正在尝试制作一个程序,它将彩色图像加载为画布上的灰度,然后将颜色返回给单击的像素。当 setrgb() 方法没有做它应该做的事情时,我被困在这里。我已经通过 getRGB() 从原始图像中复制了颜色,并正在使用 setRGB() 将其分配给新图像。我试图输出两个像素颜色值,但它们不一样。这个你能帮我吗。这是到目前为止的代码:

import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import java.applet.*;

@SuppressWarnings("serial")
public class BlackWHite extends Applet implements MouseListener 
{
    String str;
    int x=0,y=0;
    BufferedImage bimg = null;
    BufferedImage nimg = null;
    BufferedImage image=null;
    double image_width =300;
    double image_height=300;
    private Image img;

    public void init()
    {
        img = null;
        str = JOptionPane.showInputDialog(null, "Enter file location : ", 
        "Choose Image", 1);  
        BufferedImage image=null;
        try {
            image = ImageIO.read(new File(str));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //getting width and height of image
        image_width = image.getWidth();
        image_height = image.getHeight();

        BufferedImage img = image;

        //drawing a new image      
        bimg = new BufferedImage((int)image_width, (int)image_height,
                             BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D gg = bimg.createGraphics();
        gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

        nimg = new BufferedImage((int)image_width, (int)image_height,
             BufferedImage.TYPE_INT_RGB);
        Graphics2D g = nimg.createGraphics();
        g.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
        addMouseListener(this); 
    }

    public void loadImage()
    {
        try {
            img = getImage(getCodeBase(), "blackwhiteimage.jpg");
        } catch(Exception e) {
        }
    }

    public void paint(Graphics g)
    {       
        try {
            image = ImageIO.read(new File(str));
        } catch (IOException e) {
            e.printStackTrace();
        }
        convert(); //converting the image
        if (img == null)
            loadImage(); //draw
        g.drawImage(img, 0, 0, this);
        int c = image.getRGB(x,y);
        int red = (c & 0x0000FFFF) >> 16;  
        int green = (c & 0x0000FFFF) >> 8;  
        int blue = c & 0x0000FFFF;  
        c=(red << 16) | (green << 8) | blue;
        System.out.print("c="+c);
        nimg=bimg;
        nimg.setRGB(x,y,c);
        try {
            ImageIO.write(nimg, "jpg", new File("pixelcolor.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.drawImage(nimg, 0, 0, this);
        int f = nimg.getRGB(x,y);
        System.out.println("f="+f);
    }

    void convert() {
        try {
             //saving black and white image onto drive
            ImageIO.write(bimg, "jpg", new File("blackwhiteimage.jpg"));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        x = me.getX(); 
        y = me.getY();
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {     
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}
4

1 回答 1

1

根本问题是您试图在灰度颜色空间中设置图像的颜色。这不是小程序中唯一的错误,但您可以从它开始。尝试使用另一种方法将图像转换为灰度,同时将其保留在 RGBA 中,如下所示:convert a RGB image to grayscale Image减少java中的内存

于 2013-07-21T15:32:32.197 回答