2013-07-21 2 views
1

JPEG 이미지를 통해 두 이미지의 PNG/TIFF 파일을 병합하려고합니다. 다음 코드2 개의 이미지 중첩이 제대로 작동하지 않습니다.

try { 
         image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\a.jpg")); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        BufferedImage overlay = null; 
        try { 
         overlay = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\b.png")); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        // create the new image, canvas size is the max. of both image sizes 
        int w = Math.max(image.getWidth(), overlay.getWidth()); 
        int h = Math.max(image.getHeight(), overlay.getHeight()); 
        BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 

        // paint both images, preserving the alpha channels 
        Graphics2D g = combined.createGraphics(); 
        /**Set Antialias Rendering**/ 
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
          RenderingHints.VALUE_ANTIALIAS_ON); 
        /** 
         * Draw background image at location (0,0) 
         * You can change the (x,y) value as required 
         */ 
        g.drawImage(image, 0, 0, null); 

        /** 
         * Draw foreground image at location (0,0) 
         * Change (x,y) value as required. 
         */ 
        g.drawImage(overlay, 0, 0, null); 

        g.dispose(); 

        // Save as new image 
        try { 
         ImageIO.write(combined, "JPG", new File("C:\\Users\\user\\Desktop\\test\\c.jpg")); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

하지만, 내가 원하는 내용에 따라되지 형성되어 다음 최종 이미지를 사용하고 있습니다. 명확성을 위해 첨부 된 이미지를 참조하십시오. 당신이 아래에있는 이미지를 볼 수 있도록 bc

+0

PNG 이미지 a는 투명한 배경이 있어야합니다. 그거야? – Joni

+0

1) 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 2) 작은 이미지를 사용하십시오. 3) 3 번째 이미지는 처음 2 개의 이미지를 결합한 결과입니까? –

+0

1) 이것은 코드입니다 2)이 코드는 JPEG와 PNG/TIFF가 아닌 두 개의 PNG 파일과 함께 작동합니다. 3) 예 .. is is .. – ItachiUchiha

답변

1

Combine Images

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

class CombineImages { 

    public static void main(String[] args) throws Exception { 
     URL urlImage1 = new URL("http://i.stack.imgur.com/T5uTa.png"); 
     // Load the FG image (must have transparent parts) 
     final Image fgImage = ImageIO.read(urlImage1); 
     int w = fgImage.getWidth(null); 
     int h = fgImage.getHeight(null); 
     // Create a non-trasparent BG image 
     final BufferedImage bgImage = 
       new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); 
     // Create the final image 
     final BufferedImage finalImage = 
       new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = finalImage.createGraphics(); 
     g.drawImage(bgImage, 0, 0, null); 
     g.drawImage(fgImage, 0, 0, null); 
     g.dispose(); 

     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       JPanel gui = new JPanel(new GridLayout(1,0,5,5)); 

       gui.add(new JLabel(new ImageIcon(bgImage))); 
       gui.add(new JLabel(new ImageIcon(fgImage))); 
       gui.add(new JLabel(new ImageIcon(finalImage))); 

       JOptionPane.showMessageDialog(null, gui); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

이 예제의 경우 완벽하게 작동하지만, JPG 파일로 BG 이미지를 바꿀 때 dosnt가 작동하는 것 같습니다. – ItachiUchiha

+1

Huh .. 따라 가기 위해 [이 질문에] (http://stackoverflow.com/q/17816277/418556)을 참조하십시오. -쪽으로. –

+0

후속 질문에 답변 해 주셨습니다. –

관련 문제