I am trying to combine two images into a PNG / TIFF file from a JPEG image. I am using the following code
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(); }
But then the final image that is being formed does not match what I want. For clarity, refer to the attached images. 


source share