/ ** * @param filePath * source image path * @param markContent * pictures add content * @param outPath * font color output image path inside the function realized * * @param * / // to add text jpg public boolean createStringMark (URL String, String filePath, String markContent, int R & lt, int G, int B, the CHOOSEFONT String) { the ImageIcon imgIcon = new new the ImageIcon (filePath); Image theImg = imgIcon.getImage(); int width = theImg.getWidth(null) == -1 ? 200 : theImg.getWidth(null); int height = theImg.getHeight(null) == -1 ? 200 : theImg.getHeight(null); System.out.println(width); System.out.println(height); System.out.println(theImg); BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bimage.createGraphics(); //Color mycolor1 = Color.white; Color mycolor1 = newColor (255,255,255 ); g.setColor (mycolor1); g.setBackground (Color.red); G.drawImage (theImg, 0, 0, null ); // method of rendering an image, video or the canvas on the canvas. // the Font = new new font the Font ( "bold", Font.BOLD, 200 is); the Font font = new new the Font (the CHOOSEFONT, Font.BOLD, 200 is ); font = the Font (200 is ); g.setFont (font); // font, font style, size words 350 g.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // calculate the text length calculating x coordinates centered FontMetrics fm =g.getFontMetrics (font); int textWidth = fm.stringWidth (markContent); int widthX = (width - textWidth) / 2 ; // . This text represents a position in the image (x, y) is the first one you set content. g.drawString (markContent, widthX, 1130. ); Color mycolor = new new Color (R & lt, G, B); g.setColor (mycolor); g.setBackground (Color.red); // G.drawImage (theImg, 0, 0, null); the Font rotatefont = new new the Font (the CHOOSEFONT, Font.BOLD, 200 is ); rotatefont = the Font (200 is ); g.setFont (rotatefont); //Font, font style, size words 350 g.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); the FontMetrics rotatefm = g.getFontMetrics (font); int rotatetextWidth = rotatefm.stringWidth (markContent); int rotatewidthX = (width - rotatetextWidth ) / 2 ; // Videos inverted 180 degrees font g.translate (rotatewidthX rotatetextWidth +, 350 ); g.rotate (Math.toRadians ( 180 )); g.drawString (markContent, 0, 0 ); g.dispose ( ); the try { Long currentTime = System.currentTimeMillis(); String pathdate = new SimpleDateFormat("yyyy/MM/dd/HH/hh/mm/ss").format(new Date()); String path = "F:\\pic2018\\"+pathdate; File file = new File(path); if (!file.exists()) { file.mkdirs(); } QRCodeUtil qr = new QRCodeUtil(); String qrCodepath = qr.Util(url, pathdate); String outPath = path+"\\"+currenttime+".jpg"; OUT a FileOutputStream = new new a FileOutputStream (outPath); // first with a particular output file name JPEGImageEncoder Encoder = JPEGCodec.createJPEGEncoder (OUT); the JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam (bimage); param.setQuality ( 100, to true ); // encoder.encode (bimage, param); the InputStream iS = new new the FileInputStream (outPath); // Create the JPEG data through a JPEG image stream decoder flow JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder (iS); //Decoding the current JPEG data stream, the object returns BufferedImage BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage (); // get brush object Graphics G2 = buffImg.getGraphics (); // path small picture the ImageIcon QrcodeimgIcon = new new the ImageIcon (qrCodepath); // get Image object. Img = Image QrcodeimgIcon.getImage (); // . The small picture painted on the big picture, 5,300 represents your position on the small picture big picture. g2.drawImage (IMG, 50, 1560, null ); // set the color. g2.setColor (Color.WHITE); g2.dispose (); OS the OutputStream new new = a FileOutputStream (+ path "\\" + currentTime + "Zong" + "JPG." ); // record the key coder for coding image data in memory. EN = JPEGImageEncoder JPEGCodec.createJPEGEncoder (OS); en.encode (buffImg); is.close (); os.close (); the out.close (); } the catch (Exception E) { return to false ; } return to true ; } // The first parameter is the name of the external font, font size, and the second is public static the Font loadFont (String fontFileName, float the fontSize) { the try { File file = new File(fontFileName); FileInputStream aixing = new FileInputStream(file); Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing); Font dynamicFontPt = dynamicFont.deriveFont(fontSize); aixing.close(); return dynamicFontPt; } catch (Exception e)// 异常处理 { e.printStackTrace(); return new Font("宋体", Font.BOLD, 200); } } Public Font Font(int fontSize) { String the root = the System.getProperty ( "user.dir"); // item root path the Font font = ImageEdit.loadFont (the root + "/FZXBSJW.TTF", fontSize); // call return font; / / return fonts
java to write positive and negative picture fonts, and writes on a two-dimensional code images, code implementation
Guess you like
Origin www.cnblogs.com/qingmuchuanqi48/p/12079376.html
Recommended
Ranking