【问题标题】:setRGB() doesn't seem to change the colorsetRGB() 似乎没有改变颜色
【发布时间】:2013-07-10 12:18:36
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • 在保存图片之前尝试调用graphics2D.dispose()

标签: java image-manipulation image-processing


【解决方案1】:

恕我直言,你的做法似乎很奇怪……

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

例如,使用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 图形对象,它实际上可能不会绘制任何东西......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2021-08-04
    • 2013-11-01
    相关资源
    最近更新 更多