2014-02-21 1 views
1

Jframe에서이 프로그램의 출력 (세 이미지)을 인쇄해야합니다 ... 코드가 아래에 있습니다 ... "test "넷빈즈의 폴더 만은 캔트j 프레임에서 이미지의 개별 RGB 구성 요소를 페인팅하는 방법

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")); 
    } 
} 
+0

'frame.add (새 JLabel의 (새 이미지 아이콘 (redImage)))'...? – MadProgrammer

+1

모든 비트 이동이 필요 이상으로 복잡하지는 않습니까? 'redImage.setRGB (x, y, pixel & 0xffff0000); greenImage.setRGB (x, y, 픽셀 및 0xff00ff00); blueImage.setRGB (x, y, 픽셀 및 0xff0000ff); ' –

+0

@MadProgrammer_ 우리는 귀하의 코드를 시도했습니다. 그러나 그것은 작동하지 않았다. 이미지가 jFrame에 표시되지 않습니다. 제발, 당신의 도움이 시급합니다. – user3335751

답변

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); 
     } 
    } 
} 
+0

__이 JFrame 같은 원하는 위치에서 JFrame에 인쇄 할 수 없습니다. f = new JFrame(); f.setTitle ("RGB"); f.setSize (새 치수 (1360,760)); f.setVisible (true); – user3335751

+0

@ user3335751 : 다른 이미지와 마찬가지로 JFrame에 표시 할 수 있습니다. JLabel의 ImageIcon 또는 JPanel의 'paintComponent (...)'메소드로 그려진 이미지입니다. –

+0

완료 .. 페인트 구성 요소를 사용하고 원하는 출력을 얻었습니다 ... 도움을 주셔서 감사합니다 – user3335751

관련 문제