private BufferedImage compose (String fileNameFront,String fileNameBack){ BufferedImage frontImage; BufferedImage imageResult=null; try { long startFrontTime = System.currentTimeMillis(); DefaultLogger.info("StartfrontImage============="+startFrontTime); frontImage = ImageIO.read(new File(fileNameFront)); int width = frontImage.getWidth();// width int height = frontImage.getHeight();// height int[] imageArrayFirst = new int[width * height];// RGB imageArrayFirst = frontImage.getRGB(0, 0, width, height, imageArrayFirst, 0, width); frontImage.flush(); long startBackTime = System.currentTimeMillis(); DefaultLogger.info("frontImageTime============="+(startBackTime-startFrontTime)); BufferedImage backImage = ImageIO.read(new File(fileNameBack)); int widthBack = backImage.getWidth();// width int heightBack = backImage.getHeight();// height int[] imageArraySecond = new int[widthBack * heightBack]; imageArraySecond = backImage.getRGB(0, 0, widthBack, heightBack, imageArraySecond, 0, widthBack); backImage.flush(); long endBackTime = System.currentTimeMillis(); DefaultLogger.info("backImageTime============="+(endBackTime-startBackTime)); // new image imageResult = new BufferedImage(width, height * 2+3,BufferedImage.TYPE_INT_RGB); //config images Transparency Graphics2D g2d = imageResult.createGraphics(); imageResult = g2d.getDeviceConfiguration().createCompatibleImage(width, height* 2+3, Transparency.TRANSLUCENT); g2d.dispose(); imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// set above RGB imageResult.setRGB(0, height+3, width, height, imageArraySecond, 0, width);// set below RGB imageResult.flush(); long imageTime = System.currentTimeMillis(); DefaultLogger.info("ImageTime============="+(imageTime-endBackTime)); DefaultLogger.info("Form FrontImage To Image============="+(imageTime-startFrontTime)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return imageResult; } //vertical compose image private BufferedImage compose2 (String fileNameFront,String fileNameBack){ BufferedImage frontImage; BufferedImage combined=null; try { frontImage = ImageIO.read(new File(fileNameFront)); BufferedImage backImage = ImageIO.read(new File(fileNameBack)); combined = new BufferedImage(frontImage.getWidth() , frontImage.getHeight()*2, BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); g.drawImage(frontImage, 0, 0, null); g.drawImage(backImage, 0, frontImage.getHeight(), null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return combined; }
合成大小相同图片