java生成彩色附logo二维码--Java学习网

时间:2016-12-8 8:07:42

  核心提示:ava生成二维码有很多开发的jar包如zxing是谷歌开发的,这里的话我使用zxing的开发包来实现的。我们在很多项目中需要动态生成二维码,来提供给用户,这样让更多人能够很好的通过二维码来体验自己的应...

ava生成二维码有很多开发的jar包如zxing是谷歌开发的,这里的话我使用zxing的开发包来实现的。我们在很多项目中需要动态生成二维码,来提供给用户,这样让更多人能够很好的通过二维码来体验自己的应用。

下面贴出代码,已经测试通过,大家可以直接复制代码使用:

最后实现结果:

java生成二维码

代码如下:

  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Image;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.image.AffineTransformOp;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.UnsupportedEncodingException;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import javax.imageio.ImageIO;
  13. import com.google.zxing.BarcodeFormat;
  14. import com.google.zxing.EncodeHintType;
  15. import com.google.zxing.MultiFormatWriter;
  16. import com.google.zxing.WriterException;
  17. import com.google.zxing.common.BitMatrix;
  18. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  19. public class MatrixToImageWriter {
  20. private static final int IMAGE_WIDTH = 100;
  21. private static final int IMAGE_HEIGHT = 100;
  22. private static final int IMAGE_HALF_WIDTH = IMAGE_WIDTH / 2;
  23. private static final int FRAME_WIDTH = 2;
  24. private static MultiFormatWriter mutiWriter = new MultiFormatWriter();
  25. public static void encode(String content, int width, int height,
  26. String srcImagePath, String destImagePath) {
  27. try {
  28. ImageIO.write(genBarcode(content, width, height, srcImagePath),
  29. "jpg", new File(destImagePath));
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. } catch (WriterException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. private static BufferedImage genBarcode(String content, int width,
  37. int height, String srcImagePath) throws WriterException,
  38. IOException {
  39. BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH,
  40. IMAGE_HEIGHT, true);
  41. int[][] srcPixels = new int[IMAGE_WIDTH][IMAGE_HEIGHT];
  42. for (int i = 0; i < scaleImage.getWidth(); i++) {
  43. for (int j = 0; j < scaleImage.getHeight(); j++) {
  44. srcPixels[i][j] = scaleImage.getRGB(i, j);
  45. }
  46. }
  47. Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>();
  48. hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
  49. hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  50. // 生成二维码
  51. BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE,
  52. width, height, hint);
  53. // 二维矩阵转为一维像素数组
  54. int halfW = matrix.getWidth() / 2;
  55. int halfH = matrix.getHeight() / 2;
  56. int[] pixels = new int[width * height];
  57. for (int y = 0; y < matrix.getHeight(); y++) {
  58. for (int x = 0; x < matrix.getWidth(); x++) {
  59. // 左上角颜色,根据自己需要调整颜色范围和颜色
  60. if (x > 0 && x < 170 && y > 0 && y < 170) {
  61. Color color = new Color(231, 144, 56);
  62. int colorInt = color.getRGB();
  63. pixels[y * width + x] = matrix.get(x, y) ? colorInt
  64. : 16777215;
  65. }
  66. // 读取图片
  67. else if (x > halfW - IMAGE_HALF_WIDTH
  68. && x < halfW + IMAGE_HALF_WIDTH
  69. && y > halfH - IMAGE_HALF_WIDTH
  70. && y < halfH + IMAGE_HALF_WIDTH) {
  71. pixels[y * width + x] = srcPixels[x - halfW
  72. + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH];
  73. } else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
  74. && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH
  75. && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
  76. + IMAGE_HALF_WIDTH + FRAME_WIDTH)
  77. || (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH
  78. && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
  79. && y > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
  80. + IMAGE_HALF_WIDTH + FRAME_WIDTH)
  81. || (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
  82. && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
  83. && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
  84. - IMAGE_HALF_WIDTH + FRAME_WIDTH)
  85. || (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
  86. && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
  87. && y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
  88. + IMAGE_HALF_WIDTH + FRAME_WIDTH)) {
  89. pixels[y * width + x] = 0xfffffff;
  90. // 在图片四周形成边框
  91. } else {
  92. // 二维码颜色
  93. int num1 = (int) (50 - (50.0 - 13.0) / matrix.getHeight()
  94. * (y + 1));
  95. int num2 = (int) (165 - (165.0 - 72.0) / matrix.getHeight()
  96. * (y + 1));
  97. int num3 = (int) (162 - (162.0 - 107.0)
  98. / matrix.getHeight() * (y + 1));
  99. Color color = new Color(num1, num2, num3);
  100. int colorInt = color.getRGB();
  101. // 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
  102. pixels[y * width + x] = matrix.get(x, y) ? colorInt
  103. : 16777215;
  104. // 0x000000:0xffffff
  105. }
  106. }
  107. }
  108. BufferedImage image = new BufferedImage(width, height,
  109. BufferedImage.TYPE_INT_RGB);
  110. image.getRaster().setDataElements(0, 0, width, height, pixels);
  111. return image;
  112. }
  113. private static BufferedImage scale(String srcImageFile, int height,
  114. int width, boolean hasFiller) throws IOException {
  115. double ratio = 0.0; // 缩放比例
  116. File file = new File(srcImageFile);
  117. BufferedImage srcImage = ImageIO.read(file);
  118. Image destImage = srcImage.getScaledInstance(width, height,
  119. BufferedImage.SCALE_SMOOTH);
  120. // 计算比例
  121. if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) {
  122. if (srcImage.getHeight() > srcImage.getWidth()) {
  123. ratio = (new Integer(height)).doubleValue()
  124. / srcImage.getHeight();
  125. } else {
  126. ratio = (new Integer(width)).doubleValue()
  127. / srcImage.getWidth();
  128. }
  129. AffineTransformOp op = new AffineTransformOp(
  130. AffineTransform.getScaleInstance(ratio, ratio), null);
  131. destImage = op.filter(srcImage, null);
  132. }
  133. if (hasFiller) {
  134. // 补白
  135. BufferedImage image = new BufferedImage(width, height,
  136. BufferedImage.TYPE_INT_RGB);
  137. Graphics2D graphic = image.createGraphics();
  138. graphic.setColor(Color.white);
  139. graphic.fillRect(0, 0, width, height);
  140. if (width == destImage.getWidth(null))
  141. graphic.drawImage(destImage, 0,
  142. (height - destImage.getHeight(null)) / 2,
  143. destImage.getWidth(null), destImage.getHeight(null),
  144. Color.white, null);
  145. else
  146. graphic.drawImage(destImage,
  147. (width - destImage.getWidth(null)) / 2, 0,
  148. destImage.getWidth(null), destImage.getHeight(null),
  149. Color.white, null);
  150. graphic.dispose();
  151. destImage = image;
  152. }
  153. return (BufferedImage) destImage;
  154. }
  155. public static void main(String[] args) throws UnsupportedEncodingException {
  156. // 依次为内容(不支持中文),宽,长,中间图标路径,储存路径
  157. MatrixToImageWriter.encode("http://www.baidu.com/", 512, 512,
  158. "D:\\logo.png", "D:\\2013-01.jpg");
  159. }
  160. }

Java免费学习   Java自学网 http://www.javalearns.com

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java

作者:不详 来源:网络
    你是从哪里知道本网站的?
  • 网友介绍的
  • 百度搜索的
  • Google搜索的
  • 其它搜索过来的
  • 网址输错了进来的
  • 太忙了不记得了
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • java学习网(www.javalearns.com) © 2014 版权所有 All Rights Reserved.
  • Email:javalearns@163.com 站长QQ:1356121699 晋ICP备14003680号-3
  • java学习网部分内容来自网络或网友发布,如侵犯了您利益,请发邮件至:javalearns@126.com,我们尽快处理!
  • Java学习网
  • 网站统计
  • 晋公网安备 14042902000001号