如何在j帧上绘制图像的各个RGB分量

时间:2014-02-21 05:13:28

标签: java image swing awt

我需要在Jframe上打印这个程序的输出(即三个图像)...代码如下...我可以将它存储在netbeans的“test”文件夹中,但只是无法显示它..

import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;

public class RGBSpliter {
   static BufferedImage image;
   static BufferedImage redImage, greenImage, blueImage;
   static final int TYPE = BufferedImage.TYPE_INT_ARGB;

   public static void main(String[] args) throws Exception {
      image = ImageIO.read(new File("F:\\Images\\animated\\008.jpg"));
      int w = image.getWidth();
      int h = image.getHeight();
      redImage = new BufferedImage(w, h, TYPE);
      greenImage = new BufferedImage(w, h, TYPE);
      blueImage = new BufferedImage(w, h, TYPE);
      for (int y = 0; y < h; y++)
         for (int x = 0; x < w; x++) {
            int pixel = image.getRGB(x, y);
            int alpha_mask = pixel & 0xff000000;
            int red = (pixel >> 16) & 0xff;
            int green = (pixel >> 8) & 0xff;
            int blue = (pixel) & 0xff;
            redImage.setRGB(x, y, alpha_mask | (red << 16));
            greenImage.setRGB(x, y, alpha_mask | (green << 8));
            blueImage.setRGB(x, y, alpha_mask | blue);
         }
      String format = "png";
      ImageIO.write(redImage, format, new File("image_red.png"));
      ImageIO.write(greenImage, format, new File("image_green.png"));
      ImageIO.write(blueImage, format, new File("image_blue.png"));
   }
}

1 个答案:

答案 0 :(得分:0)

您的代码适合我:

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.net.URL;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class RGBSpliter {
   static BufferedImage image;
   static BufferedImage redImage, greenImage, blueImage;
   static final int TYPE = BufferedImage.TYPE_INT_ARGB;

   public static void main(String[] args) throws Exception {
      String imgPath =
           "https://duke.kenai.com/china/.Midsize/DragonGoldenMedium.jpg.png";
      URL imgUrl = new URL(imgPath);
      image = ImageIO.read(imgUrl);


      int w = image.getWidth();
      int h = image.getHeight();
      redImage = new BufferedImage(w, h, TYPE);
      greenImage = new BufferedImage(w, h, TYPE);
      blueImage = new BufferedImage(w, h, TYPE);
      for (int y = 0; y < h; y++) {
         for (int x = 0; x < w; x++) {
            int pixel = image.getRGB(x, y);
            int alpha_mask = pixel & 0xff000000;
            int red = (pixel >> 16) & 0xff;
            int green = (pixel >> 8) & 0xff;
            int blue = (pixel) & 0xff;
            redImage.setRGB(x, y, alpha_mask | (red << 16));
            greenImage.setRGB(x, y, alpha_mask | (green << 8));
            blueImage.setRGB(x, y, alpha_mask | blue);
         }
      }
      // String format = "png";

      Image[] images = {image, redImage, greenImage, blueImage};
      for (Image localImage : images) {
         ImageIcon icon = new ImageIcon(localImage);
         JOptionPane.showMessageDialog(null, icon);
      }
   }
}