積跬步,聚小流------java資訊產生圖片

來源:互聯網
上載者:User

標籤:java   學習記錄   圖片   

需求:
是在做認證的時候碰到的這個問題。當時需求是可以進行線上列印認證,第一次進行的操作是直接列印html,並且已經排好版(用jqprint外掛程式)進行列印,在列印時碰到了相容的問題,另外由於背景圖片載入較文字載入的慢,則出現先載入文字後出現圖片的現象,則顯的很不專業,遂採用了將資訊轉化為圖片,然後直接列印圖片,不論怎麼樣進行列印,都不會改變,而且可以匯出圖片,對圖片進行更為精細的操作。
代碼編寫:
先不多說 先上代碼:
package com.jzba.utils;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.Transparency;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import java.util.Random;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import com.sun.image.codec.jpeg.ImageFormatException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImgUtils {private static int newWidth=0,newHeight=0,newX=0,newY=0;public static void main(String[] args) throws Exception{--1、本地測試String over="D:/zz/1/over.png";Color color = new Color(100,100,100);ImageUtil iu=new ImageUtil();Map map=iu.getImageSizeByBufferedImage("D:/zz/ceshi.png");--備忘:見代碼下方int width=(Integer)map.get("width");int height=(Integer)map.get("height");float bili=width/height;if (width>height) {newWidth=660;newHeight=height*660/width;newX=600;newY=1160+330-newHeight/2;}else{newHeight=660;newWidth=width*660/height;newY=1160;newX=600+330-newWidth/2;}//String temp1=createCard("D:/zz/1/background.png","D:/zz/1/ceshi.png","D:/zz/1/code.bmp","D:/zz/1/temp1.png",newX,newY,600,1210,newWidth,newHeight,250,250);String temp1=createCard("D:/zz/1/background.png","D:/zz/1/ceshi2.jpg","D:/zz/1/code.bmp","D:/zz/1/temp1.png",100,100,600,1210,newWidth,newHeight,250,250);ImageIcon imgIcon = new ImageIcon(temp1);   Image theImg = imgIcon.getImage();   int w = theImg.getWidth(null);   int h = theImg.getHeight(null);   BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);   // 擷取Graphics2D   Graphics2D g = bufImg.createGraphics();   g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);   // 畫圖   bufImg = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);   g.dispose();   g = bufImg.createGraphics();   g.setStroke(new BasicStroke(1));   g.drawImage(theImg, 0, 0, w, h, null);//背景圖起始位置   g.setColor(color);   Font nf = loadFont("D:/zz/simhei.ttf",60);   Font nf2 = loadFont("D:/zz/simhei.ttf",50);       g.setFont(nf);   g.drawString("秘  密",2500,1040);//   g.setFont(nf2);   g.drawString("秘  密",1920,1190);//   g.drawString("秘  密", 1920, 1290);//   g.drawString("秘  密", 1920, 1390);//   g.drawString("秘  密", 1920, 1490);//   g.drawString("秘  密 ", 1920, 1590);//   g.drawString("秘  密", 1920, 1700);//   g.drawString("秘  密", 1920, 1800);//   //釋放對象   File tofile=new File(over);   ImageIO.write(bufImg, "png", tofile);   g.dispose();}/** *  * 將所要添加的圖片整合在一起進行添加 * @param backsPath 背景圖片的路徑 * @param titleImgPath 縮圖圖片的路徑 * ... *  * @return toPath 合成後圖片的路徑         *          * */public static String createCard(String backsPath,String titleImgPath,String codeImgPath,String toPath,int x,int y,int newx,int newy,int widthTitle,int heightTitle,int codeWidth,int codeHeight){ try {InputStream imageBack=new FileInputStream(backsPath);//背景圖        InputStream imageTitle=new FileInputStream(titleImgPath);//縮圖圖片        InputStream imageCode=new FileInputStream(codeImgPath);//二維碼圖片        BufferedImage backImg=ImageIO.read(imageBack);        BufferedImage titleImg=ImageIO.read(imageTitle);        BufferedImage codeImg=ImageIO.read(imageCode);        System.out.println("*****Graphics****"+backsPath);        Graphics g=backImg.getGraphics();        System.out.println("*****Graphics****1");        g.drawImage(titleImg,x,y,widthTitle,heightTitle,null);        System.out.println("*****Graphics****2");        g.drawImage(codeImg,newx,newy,codeWidth,codeHeight,null);        System.out.println("*****Graphics****3");        OutputStream outImage=new FileOutputStream(toPath);        System.out.println("*****Graphics****4");//        JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(outImage);--這裡存在問題//        System.out.println("*****Graphics****5");--在window下可以,在linux下無法進行//enc.encode(backImg);        File tofile=new File(toPath);        ImageIO.write(backImg, "png", tofile);//System.out.println("*****"+backsPath);        imageBack.close();//關閉輸出連接埠        imageTitle.close();        imageCode.close();        outImage.close();} catch (Exception e) {e.printStackTrace();}return toPath;}/** * 將所要添加的文本資訊整合在一起進行添加 *  *  * **/public static String addCardWord(String filePath,String toPath,int fontsize, Color markContentColor,String fontPath,String code,String type,String artist,String artname,String description,String material,String artisttype,String recordescription,String releaseDate,int codex,int codey,int typex,int typey,int artistx,int artisty,int artnamex,int artnamey,int descriptionx,int descriptiony,int materialx,int materialy,int artisttypex,int artisttypey,int recordescriptionx,int recordescriptiony)throws Exception{ImageIcon imgIcon = new ImageIcon(filePath);   Image theImg = imgIcon.getImage();   int w = theImg.getWidth(null);   int h = theImg.getHeight(null);   BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);   // 擷取Graphics2D   Graphics2D g = bufImg.createGraphics();   g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);   // 畫圖   bufImg = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);   g.dispose();   g = bufImg.createGraphics();   g.setStroke(new BasicStroke(1));   g.drawImage(theImg, 0, 0, w, h, null);//背景圖起始位置   g.setColor(markContentColor);   Font nf = loadFont(fontPath,fontsize);       g.setFont(nf);   g.drawString(code, codex, codey);   g.drawString(type, typex, typey);   g.drawString(artist, artistx, artisty);   g.drawString(artname, artnamex, artnamey);   g.drawString(description, descriptionx, descriptiony);   g.drawString(material, materialx, materialy);   g.drawString(artisttype, artisttypex, artisttypey);   g.drawString(recordescription, recordescriptionx, recordescriptiony);   //釋放對象   File tofile=new File(toPath);   ImageIO.write(bufImg, "png", tofile);   g.dispose();   return toPath;}/** * 向圖片裡面添加圖片 * @param backsPath 背景圖片路徑 * @param upPath 添加圖片路徑 * @param toPath 產生圖片路徑 * @param x 所添加圖片在背景圖片上的橫向位移,以左上方為標準 * @param y 所添加圖片在背景圖片上的豎向位移,以左上方為標準 * @param width 所添加圖片的寬度 * @param height 所添加圖片的高度 * @return toPath String 返回產生圖片的路徑 *  * **/    public static String addImg2Img(String backsPath,String upPath,String toPath,int x,int y,int width,int height){           try {               InputStream imagein=new FileInputStream(backsPath);               InputStream imagein2=new FileInputStream(upPath);               BufferedImage image=ImageIO.read(imagein);//讀取背景圖片               BufferedImage image2=ImageIO.read(imagein2);//讀取要添加的圖片               Graphics g=image.getGraphics();//將背景圖片作為畫圖面板               g.drawImage(image2,x,y,width,height,null);//在背景圖片的基礎上進行畫圖,將待添加圖片加入               OutputStream outImage=new FileOutputStream(toPath);//開啟匯出圖片路徑               JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(outImage);               enc.encode(image);//將產生的圖片輸出               imagein.close();//關閉開啟的匯入、匯出通道               imagein2.close();               outImage.close();   } catch (Exception e) {       e.printStackTrace();   }           return toPath;    }    /** * 向圖片裡面添加文字 * @param filePath 背景圖片路徑 * @param toPath 產生圖片路徑 * @param markContent 添加的常值內容 * @param fontsize 添加文本的字型大小 * @param markContentColor 添加文本的顏色 * @param fontPath 添加文本的字型 * @param x 所添加圖片在背景圖片上的橫向位移,以左上方為標準 * @param y 所添加圖片在背景圖片上的豎向位移,以左上方為標準 * @return toPath String 返回產生圖片的路徑 *  * **/    public static String addWord2Img(String filePath,String toPath, String markContent,int fontsize, Color markContentColor,String fontPath,int x,int y) throws Exception {   ImageIcon imgIcon = new ImageIcon(filePath);   Image theImg = imgIcon.getImage();   int w = theImg.getWidth(null);   int h = theImg.getHeight(null);   BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);   // 擷取Graphics2D   Graphics2D g = bufImg.createGraphics();   g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);   // 畫圖   bufImg = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);   g.dispose();   g = bufImg.createGraphics();   g.setStroke(new BasicStroke(1));   g.drawImage(theImg, 0, 0, w, h, null);//背景圖起始位置   g.setColor(markContentColor);//給添加的文本進行顏色設定   Font nf = loadFont(fontPath,fontsize);       g.setFont(nf);//對添加的文本進行字型和字型大小設定   g.drawString(markContent, x, y);//將文本添加到圖片指定的位置   //釋放對象   File tofile=new File(toPath);//開啟匯出通道   ImageIO.write(bufImg, "png", tofile);//進行畫圖   g.dispose();   return toPath;  }    /** * 對添加文本的字型和字型大小進行設定 * @param fontFileName 外部字型名 * @param fontsize 添加文本的字型大小 * @param markContentColor 添加文本的顏色 * @return dynamicFontPt Font 對文本的相應設定 *  * **/public static Font loadFont(String fontFileName, float fontSize) throws Exception  //第一個參數是外部字型名,第二個是字型大小{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;}}
備忘:在代碼中調用到了上一篇文章中擷取圖片尺寸的相應類,用在這裡的目的是,針對不同的作品,可能是橫向圖片,也可能是豎條圖片,為了保證圖片不會變形失真,則將圖片顯示在固定的一個地區內,像這裡:
預設了一段660*660的地區,首先進行判定要添加圖片是橫條還是豎條,如果是橫條,則寬度是大於高度的,則設定大的寬度為660,相應的根據比例:660/寬度=要求的高度/實際高度,則可以得到相應的高度值,而且此高度值肯定是小於660的,因為是橫條,所以寬度的位移就是指定地區的相應位移,這裡為600px,而高度位移,則應該是添加圖片的左上方的位移,為背景圖片的高度一半減去圖片當前高度的一半;同理如果是豎條,則高度是大於寬度的,則設定大的高度為660,以此類推,得到相應的位移和尺寸。從而實現的效果是什麼樣子呢,我們來簡單看下:(這是高度大於寬度)
(這是寬度大於高度)

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

積跬步,聚小流------java資訊產生圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.