0

你能告诉我为什么像素不会设置为红色吗

    Color myColor = new Color(255, 0, 0);
    int rgb = myColor.getRGB();
    String fileName = Config.IMAGEFILEPATH + "first_nodal_domain "
                + "full.png";

        BufferedImage bi = new BufferedImage(AZIMUTH_RES, ELEVATION_RES, BufferedImage.TYPE_USHORT_GRAY);
        for (int i = 0; i < AZIMUTH_RES; i++){
            for (int j = 0; j < ELEVATION_RES; j++){
                bi.setRGB(i,j,(255 << 16) + (255 << 8) + 255);
            }
        }
         for (Point draw: shadedPoints){
            bi.setRGB(draw.x, draw.y, rgb);
        }
         BufferedImage scaledImage = new BufferedImage(
            1000, 1000, BufferedImage.TYPE_USHORT_GRAY);

    // Paint scaled version of image to new image
    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(bi, 0, 0, 1000, 1000, null);
        try {
            // write out image to file as .png
            ImageIO.write(scaledImage, "png", new File(fileName));
        } catch (IOException ex) {
            Logger.getLogger(NodalDomainsDrawing.class.getName()).log(Level.SEVERE, null, ex);
        }

       bi.flush();

提前致谢。

4

1 回答 1

2

恕我直言,你似乎在做这件事很奇怪......

与其尝试直接绘制到像素级别,不如使用GraphicsAPI 功能。

Graphics#fillRect例如,使用然后循环和设置每个像素,清除图像将明显更快。

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class TestImage02 {

    public static void main(String[] args) {
        Color myColor = new Color(255, 0, 0);
//        int rgb = myColor.getRGB();

        List<Point> shadedPoints = new ArrayList<>(25);
        for (int index = 0; index < 100; index++) {
            shadedPoints.add(new Point(index, index));
        }

        BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_GRAY);
        Graphics2D g2d = bi.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, 100, 100);
//        for (int i = 0; i < 100; i++) {
//            for (int j = 0; j < 100; j++) {
//                bi.setRGB(i, j, (255 << 16) + (255 << 8) + 255);
//            }
//        }
        g2d.setColor(myColor);
        for (Point draw : shadedPoints) {
//            bi.setRGB(draw.x, draw.y, rgb);
            g2d.drawLine(draw.x, draw.y, 1, 1);
        }
        g2d.dispose();
        BufferedImage scaledImage = new BufferedImage(
                1000, 1000, BufferedImage.TYPE_USHORT_GRAY);

        // Paint scaled version of image to new image
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(bi, 0, 0, 1000, 1000, null);
        graphics2D.dispose();
        try {
            // write out image to file as .png
            ImageIO.write(scaledImage, "png", new File("Test.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        bi.flush();
    }
}

我运行了您的原始代码(进行了修改以使其工作)并且工作正常,但我发布了一些额外的代码来Graphics代替。

您应该确保您正在调用Graphics#dispose. 在不同的操作系统上,Graphics对象的行为可能不同,这意味着有时,在您dispose使用图形对象之前,它实际上可能不会绘制任何东西......

于 2013-07-12T03:27:34.307 回答