添加二维码 二维码加文字,logo 以及X11FontManager报错的处理
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>zxing</artifactId>
</dependency>
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.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; 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.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Hashtable; import java.util.Map; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import com.gexun.common.util.CommonUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class CreQrImgUtil { public static final int WIDTH = 300; public static final int HEIGHT = 300; public static final String FORMAT = "png"; public static final int FONTSIZE = 18;
//创建普通二维码 不带字和logo的 public static File creQrImg(String text, String key, String path, int width, int height, String format){ if(width==0){ width = WIDTH; } if(height==0){ height = HEIGHT; } if(format==null||format==""){ format = FORMAT; } File pathFile = null; if(path!=null&&path!=""){ pathFile = new File(path); if(!pathFile.isDirectory()){ pathFile.mkdirs(); } } Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); width = bitMatrix.getWidth(); height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB()); } } File file = null; if(pathFile==null){ file = File.createTempFile(key+"_", ".png"); }else{ file = File.createTempFile(key+"_", ".png", pathFile); } ImageIO.write(image, format, file); return file; } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //创建带logo 文字说明的二维码 public static File creQrImgWithLogoText(String text, String key, String path, int width, int height, String format,String title,InputStream imagest,String toptitle){ if(width==0){ width = WIDTH; } if(height==0){ height = HEIGHT; } if(format==null||format==""){ format = FORMAT; } File pathFile = null; if(path!=null&&path!=""){ pathFile = new File(path); if(!pathFile.isDirectory()){ pathFile.mkdirs(); } } Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); width = bitMatrix.getWidth(); height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB()); } }
//二维码底部加字 if(CommonUtils.isNotEmpty(title)){ //如果将title 高增加1个字体大小 //计算文字开始的位置 //x开始的位置:(图片宽度-字体大小*字的个数)/2 int startX = (width-(FONTSIZE*title.length()))/2; //y开始的位置:图片高度-(图片高度-图片宽度)/2 int startY = height-(height-width)/2; Graphics g = image.createGraphics(); g.drawImage(image, 0, 0, width, height, null); g.setColor(Color.blue); g.setFont(new Font(null, 1, FONTSIZE)); g.drawString(title, startX, startY); }
//二维码头部加字 if(CommonUtils.isNotEmpty(toptitle)){ //如果将title 高增加1个字体大小 //计算文字开始的位置 //x开始的位置:(图片宽度-字体大小*字的个数)/2 int startX = (width-(FONTSIZE*toptitle.length()))/2; //y开始的位置:图片高度-(图片高度-图片宽度)/2 int startY = (height-width); Graphics g = image.createGraphics(); g.drawImage(image, 0, 0, width, height, null); g.setColor(Color.blue); g.setFont(new Font(null, 1, FONTSIZE)); g.drawString(toptitle, startX, startY); }
//加logo if(CommonUtils.isNotEmpty(imagest)){ /** * 直接根据流读取Logo图片 */ BufferedImage logo = ImageIO.read(imagest); Graphics2D g = image.createGraphics(); int widthLogo = image.getWidth()/6; // int heightLogo = image.getHeight()/logoConfig.getLogoPart(); int heightLogo = image.getWidth()/6; //保持二维码是正方形的 // 计算图片放置位置 int x = (image.getWidth() - widthLogo) / 2; int y = (image.getHeight() - heightLogo) / 2 ; //开始绘制图片 g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10); g.setStroke(new BasicStroke(2)); g.setColor(Color.WHITE); g.drawRect(x, y, widthLogo, heightLogo); g.dispose(); } File file = null; if(pathFile==null){ file = File.createTempFile(key+"_", ".png"); }else{ file = File.createTempFile(key+"_", ".png", pathFile); } ImageIO.write(image, format, file); return file; } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //单纯加字 public static void pressText(String pressText, String newImg, String targetImg, int fontStyle, Color color, int fontSize, int width, int height) { //计算文字开始的位置 //x开始的位置:(图片宽度-字体大小*字的个数)/2 int startX = (width-(fontSize*pressText.length()))/2; //y开始的位置:图片高度-(图片高度-图片宽度)/2 int startY = height-(height-width)/2; try { File file = new File(targetImg); Image src = ImageIO.read(file); int imageW = width; int imageH = height; BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(src, 0, 0, imageW, imageH, null); g.setColor(color); g.setFont(new Font(null, fontStyle, fontSize)); g.drawString(pressText, startX, startY); g.dispose(); FileOutputStream out = new FileOutputStream(newImg); ImageIO.write(image, "JPEG", out); // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); // encoder.encode(image); out.close(); System.out.println("image press success"); } catch (Exception e) { System.out.println(e); } } //单纯加logo public static void addLogo_QRCode(File qrPic, File logoPic) { try { if (!qrPic.isFile() || !logoPic.isFile()) { System.out.print("file not find !"); System.exit(0); } /** * 读取二维码图片,并构建绘图对象 */ BufferedImage image = ImageIO.read(qrPic); Graphics2D g = image.createGraphics(); /** * 读取Logo图片 */ BufferedImage logo = ImageIO.read(logoPic); int widthLogo = image.getWidth()/6; // int heightLogo = image.getHeight()/logoConfig.getLogoPart(); int heightLogo = image.getWidth()/6; //保持二维码是正方形的 // 计算图片放置位置 int x = (image.getWidth() - widthLogo) / 2; int y = (image.getHeight() - heightLogo) / 2 ; //开始绘制图片 g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10); g.setStroke(new BasicStroke(2)); g.setColor(Color.WHITE); g.drawRect(x, y, widthLogo, heightLogo); g.dispose(); ImageIO.write(image, "jpeg", new File("D:/newPic.jpg")); } catch (Exception e) { e.printStackTrace(); } } public static void addLogo_QRCode(File qrPic,InputStream logostream) { try { if (!qrPic.isFile()) { System.out.print("file not find !"); System.exit(0); } /** * 读取二维码图片,并构建绘图对象 */ BufferedImage image = ImageIO.read(qrPic); Graphics2D g = image.createGraphics(); /** * 读取Logo图片 */ BufferedImage logo = ImageIO.read(logostream); int widthLogo = image.getWidth()/6; // int heightLogo = image.getHeight()/logoConfig.getLogoPart(); int heightLogo = image.getWidth()/6; //保持二维码是正方形的 // 计算图片放置位置 int x = (image.getWidth() - widthLogo) / 2; int y = (image.getHeight() - heightLogo) / 2 ; //开始绘制图片 g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10); g.setStroke(new BasicStroke(2)); g.setColor(Color.WHITE); g.drawRect(x, y, widthLogo, heightLogo); g.dispose(); ImageIO.write(image, "png", new File("D:/pic/newPic.png")); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { String savePath = "D:/pic"; String text = "http://www.baidu.com"; // File file = creQrImg(text,"s1", savePath, 0, 0, null); String logo = "D:/pic/p.jpg"; //creQrImgWithLogoText(text,"s1", savePath, 300, 320, null,"吴昊是个大帅比",logo); String old ="D:/pic/s1_2825635464965211529.png"; String n1 ="D:/pic/test.png"; try { URL url = new URL("http://192.168.2.250:85/2017/11/06/17/1509959731033234.jpg"); URLConnection con = url.openConnection(); InputStream imagest = con.getInputStream(); File file = creQrImgWithLogoText(text,"s1", savePath, 300, 336, null,"吴昊是个大帅比",null,"圣天生鲜买菜就是方便!"); FileInputStream in = null; in = new FileInputStream(file); addLogo_QRCode(file,imagest); file.deleteOnExit(); //CoreConfig.extendFilePath } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // pressText("郑国荣",n1,old,1,Color.blue,18,400,450); //addLogo_QRCode(new File(n1),new File(logo)); } }
以上是 二维码加字 logo的代码 本地测试没问题 但是部署到linux服务器上出现问题:
could not initialize class sun.awt.X11FontManager 问题的思考:
首先我本地是没问题的 部署到linux服务器上有问题 百度了一下相关,首先猜想是服务器上缺少字体文件,
随后从本地拿了宋体文件添加到服务器,复制到linux的/usr/share/fonts文件夹下面,然后运行fc-cache -fv命令将字体重载一下
source /etc/profile 或者reboot重启服务器
查看字体为中文的 fc-list :lang=zh
但是graphics.drawstring()仍然报错
这次报的是Handler processing failed; nested exception is java.lang.UnsatisfiedLinkError: /data/env/jdk1.7.0_80/jre/lib/i386/libfontmanager.so
百度相关可能是jdk版本冲突 查看了linux为64位 jdk为32位 估计问题在这 重新安装了64位的jdk 问题得以解决