java image drawing to another image

public static String exportImg(PiousCard piousCard,HttpServletRequest request){  
        try {  
            //1.jpg is the path to your main image  
        	String jzz = request.getSession().getServletContext().getRealPath(File.separator) + ReadConfig.getConfigValue("jzz") + File.separator;
        	InputStream is = new FileInputStream(jzz +"jzz.jpg");  
              
              
            //Create a JPEG data stream decoder from a JPEG image stream  
            JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);  
            //Decode the current JPEG data stream and return the BufferedImage object  
            BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();  
            // get the brush object  
            Graphics g = buffImg.getGraphics();  
              
            //Create the image you want to attach.  
            // path of the small image  
            String uploadDir = request.getSession().getServletContext().getRealPath(File.separator) + ReadConfig.getConfigValue("uploadRoot")
            		+ File.separator+ ReadConfig.getConfigValue("barcode") + File.separator;
            String imgUrl = uploadDir+piousCard.getTwoCode();
            ImageIcon imgIcon = new ImageIcon(uploadDir+piousCard.getTwoCode());   
              
            //Get the Image object.  
            Image img = imgIcon.getImage();  
            
            // Draw the small picture onto the big picture.  
            //5,300 . Indicates the position of your small image on the larger image.  
            g.drawImage(img,105,405,null);  
              
            //Set the color.  
            g.setColor(Color.BLACK);  
              
              
            //The last parameter is used to set the font size  
            Font f = new Font("宋体",Font.PLAIN,11);  
            Color mycolor = Color.BLACK;//new Color(0, 0, 255);  
            g.setColor(mycolor);  
            g.setFont(f);  
              
            //10,20 represents the position (x,y) of this text on the picture. The first one is the content you set.  
            g.drawString("Scan to verify authenticity",100,470);
             
             
            Graphics2D tip = buffImg.createGraphics();
            tip.setColor(Color.BLACK);
            //set font
            Font tipFont = new Font("宋体", Font.PLAIN, 14);
            tip.setFont(tipFont);
            //Content, left and right position, up and down position
            tip.drawString(piousCard.getPersonName(), 135, 285);
            tip.drawString(piousCard.getYear(), 160, 323);
            tip.drawString(piousCard.getCardNum(), 553, 82);
            SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            tip.drawString (bartDateFormat.format (piousCard.getIssueDate ()), 590, 460);
            g.dispose();
            
            OutputStream os;  
          
//            os = new FileOutputStream("d:/union.jpg");  
            String upJzzUrl = request.getSession().getServletContext().getRealPath(File.separator) + ReadConfig.getConfigValue("uploadRoot")
            		+ File.separator;
            String shareFileName = piousCard.getCardNum()+ ".jpg";  
            os = new FileOutputStream(upJzzUrl+shareFileName);  
             //Create key encoder, used to encode image data in memory.            
            JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);  
            en.encode(buffImg);           
              
            is.close();  
            os.close();  
            return shareFileName;
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace ();  
        } catch (ImageFormatException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace ();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace ();  
        }
		return null;  
          
    }

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974627&siteId=291194637