(Java)如何使用setRGB()和缓冲图像

时间:2018-04-27 20:32:33

标签: java bufferedimage

在我的addMarking方法中获取空指针异常。

我是新来的缓冲图片,所以我不能完全确定问题是什么。我有一个单独的类,将图像分成3x3数组并用它调用findEnds()。当findEnds在3x3数组中找到正确的条件时,它会调用addMarking并使用坐标在图片上创建一个红点。 注意:以前我尝试使用2dGraphic在bufferedImage上编写图片,但是试图解决这个问题并尝试使用setRGB手动复制像素(尚未编写此方法)。 注意:我不能用原始文件制作缓冲图像的原因是文件的黑白颜色类型不是TYPE_INT_RGB。

错误:

Exception in thread "main" java.lang.NullPointerException
    at findFeatures.addMarking(findFeatures.java:43)
    at getImage.getColor(getImage.java:44)
    at fingerprintDriver.main(fingerprintDriver.java:15)

getImage是一个将照片分成3x3数组的类,它还将请求数组的坐标返回给addMarking类。

fingerprintDriver:

/**
 * Driver for the ElevationScanner Class
 * 
 * @author benjamincole
 * @version 4/23/18
 */
public class fingerprintDriver {
    public static void main(String[] args) {
        String fileName = JOptionPane.showInputDialog("Please enter a name for the file");
        getImage test = new getImage(fileName);
        findFeatures find = new findFeatures();
        find.imageConstructor();
        test.getColor();

    }
}

findFeatures类:

 /**
     * This class tests a 3x3 array. The method findEnds tests this array for
     * two black pixels. If this condition is found it is an end and the x  
     * and y-coords are marked.
     * 
     * @author benjamincole
     *
 */
public class findFeatures {
    BufferedImage markedImage;
    int counter;
    public void imageConstructor() { // creates image to add markings to
            markedImage = new BufferedImage(getImage.width, getImage.height, BufferedImage.TYPE_INT_RGB);
    }

    public void findEnds(boolean[][] colorArray) { // finds the ends of arches
        counter = 0;
        System.out.println();
        for (int x = 0; x < 3; x++) {
            System.out.println();
            for (int y = 0; y < 3; y++) {
                System.out.print(" " + colorArray[x][y]);
                if (colorArray[x][y])
                    counter++;
            }
        }
            if (counter == 2 || counter == 1) { // if only 2 or less pixels are
                                                // black in the 3x3
                System.out.println(getImage.xCord + " " + getImage.yCord);
                System.out.println(" end found"); // end has been found
                addMarking(getImage.getXCord(), getImage.getYCord(), Color.red.getRGB());
            }   
        }

    public void addMarking(int x, int y, int rgb) { // adds red `marking to image`
        markedImage.setRGB(x, y, rgb); //ERROR HERE! (Null Pointer)
        markedImage.setRGB(x + 1, y + 1, rgb);
        markedImage.setRGB(x + 2, y + 2, rgb);
        markedImage.setRGB(x, y + 2, rgb);
        markedImage.setRGB(x + 2, y, rgb);
    }

0 个答案:

没有答案