普通二维码生成

ゝ一纸荒年。 2022-06-09 14:10 216阅读 0赞

依赖:ZXing-core-3.2.1.jar
1、二维码生成

  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import javax.imageio.ImageIO;
  6. import com.google.zxing.common.BitMatrix;
  7. /* * 生成二维码 */
  8. public class MatrixToImageWriter {
  9. private static final int BLACK = 0xFF000000;
  10. private static final int WHITE = 0xFFFFFFFF;
  11. private MatrixToImageWriter() {
  12. }
  13. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  14. int width = matrix.getWidth();
  15. int height = matrix.getHeight();
  16. BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  17. for (int x = 0; x < width; x++) {
  18. for (int y = 0; y < height; y++) {
  19. image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  20. }
  21. }
  22. return image;
  23. }
  24. public static void writeToFile(BitMatrix matrix, String format, File file)throws IOException {
  25. BufferedImage image = toBufferedImage(matrix);
  26. if (!ImageIO.write(image, format, file)) {
  27. throw new IOException("Could not write an image of format "+ format + " to " + file);
  28. }
  29. }
  30. public static void writeToStream(BitMatrix matrix, String format,OutputStream stream) throws IOException {
  31. BufferedImage image = toBufferedImage(matrix);
  32. if (!ImageIO.write(image, format, stream)) {
  33. throw new IOException("Could not write an image of format "+ format);
  34. }
  35. }
  36. }

2、解析二维码

  1. import java.awt.Graphics2D;
  2. import java.awt.geom.AffineTransform;
  3. import java.awt.image.BufferedImage;
  4. import com.google.zxing.LuminanceSource;
  5. /* * 解析二维码: */
  6. public class BufferedImageLuminanceSource extends LuminanceSource {
  7. private final BufferedImage image;
  8. private final int left;
  9. private final int top;
  10. public BufferedImageLuminanceSource(BufferedImage image) {
  11. this(image, 0, 0, image.getWidth(), image.getHeight());
  12. }
  13. public BufferedImageLuminanceSource(BufferedImage image, int left, int top,int width, int height) {
  14. super(width, height);
  15. int sourceWidth = image.getWidth();
  16. int sourceHeight = image.getHeight();
  17. if (left + width > sourceWidth || top + height > sourceHeight) {
  18. throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
  19. }
  20. for (int y = top; y < top + height; y++) {
  21. for (int x = left; x < left + width; x++) {
  22. if ((image.getRGB(x, y) & 0xFF000000) == 0) {
  23. image.setRGB(x, y, 0xFFFFFFFF); // = white
  24. }
  25. }
  26. }
  27. this.image = new BufferedImage(sourceWidth, sourceHeight,BufferedImage.TYPE_BYTE_GRAY);
  28. this.image.getGraphics().drawImage(image, 0, 0, null);
  29. this.left = left;
  30. this.top = top;
  31. }
  32. @Override
  33. public byte[] getRow(int y, byte[] row) {
  34. if (y < 0 || y >= getHeight()) {
  35. throw new IllegalArgumentException("Requested row is outside the image: " + y);
  36. }
  37. int width = getWidth();
  38. if (row == null || row.length < width) {
  39. row = new byte[width];
  40. }
  41. image.getRaster().getDataElements(left, top + y, width, 1, row);
  42. return row;
  43. }
  44. @Override
  45. public byte[] getMatrix() {
  46. int width = getWidth();
  47. int height = getHeight();
  48. int area = width * height;
  49. byte[] matrix = new byte[area];
  50. image.getRaster().getDataElements(left, top, width, height, matrix);
  51. return matrix;
  52. }
  53. @Override
  54. public boolean isCropSupported() {
  55. return true;
  56. }
  57. @Override
  58. public LuminanceSource crop(int left, int top, int width, int height) {
  59. return new BufferedImageLuminanceSource(image, this.left + left,this.top + top, width, height);
  60. }
  61. @Override
  62. public boolean isRotateSupported() {
  63. return true;
  64. }
  65. @Override
  66. public LuminanceSource rotateCounterClockwise() {
  67. int sourceWidth = image.getWidth();
  68. int sourceHeight = image.getHeight();
  69. AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0,0.0, sourceWidth);
  70. BufferedImage rotatedImage = new BufferedImage(sourceHeight,sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
  71. Graphics2D g = rotatedImage.createGraphics();
  72. g.drawImage(image, transform, null);
  73. g.dispose();
  74. int width = getWidth();
  75. return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
  76. }
  77. }

3、解析输出二维码信息

  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.imageio.ImageIO;
  6. import com.google.zxing.Binarizer;
  7. import com.google.zxing.BinaryBitmap;
  8. import com.google.zxing.EncodeHintType;
  9. import com.google.zxing.LuminanceSource;
  10. import com.google.zxing.MultiFormatReader;
  11. import com.google.zxing.Result;
  12. import com.google.zxing.common.HybridBinarizer;
  13. import erWeiMa.BufferedImageLuminanceSource;
  14. public class jiexi {
  15. public static void main(String[] args) {
  16. try {
  17. MultiFormatReader formatReader = new MultiFormatReader();
  18. String filePath = "C:\\Users\\zk\\Desktop\\qrCode.jpg";
  19. File file = new File(filePath);
  20. BufferedImage image = ImageIO.read(file);
  21. LuminanceSource source = new BufferedImageLuminanceSource(image);
  22. Binarizer binarizer = new HybridBinarizer(source);
  23. BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
  24. Map hints = new HashMap();
  25. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  26. Result result = formatReader.decode(binaryBitmap, hints);
  27. System.out.println("result = " + result.toString());
  28. System.out.println("resultFormat = " + result.getBarcodeFormat());
  29. System.out.println("resultText = " + result.getText());
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

4、生成二维码图片

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import com.google.zxing.BarcodeFormat;
  6. import com.google.zxing.EncodeHintType;
  7. import com.google.zxing.MultiFormatWriter;
  8. import com.google.zxing.WriterException;
  9. import com.google.zxing.common.BitMatrix;
  10. import erWeiMa.MatrixToImageWriter;
  11. /* * 二维码生成 */
  12. public class main {
  13. public static void main(String[] args) {
  14. try {
  15. String content = "http://www.baidu.com";
  16. String path = "C:\\Users\\zk\\Desktop";
  17. MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
  18. Map hints = new HashMap();
  19. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  20. BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
  21. File file1 = new File(path, "qrCode.jpg");
  22. MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
  23. } catch (WriterException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

发表评论

表情:
评论列表 (有 0 条评论,216人围观)

还没有评论,来说两句吧...

相关阅读

    相关 生成

    二维码生成源码 开发前准备第三方包:QRCode.jar 下载链接:[QRCode.jar百度云下载链接][QRCode.jar] 提取码:1pvs ![在这里插入图

    相关 swift生成,扫描

    ~~~写在前面的话~~~ 我之前打算做一个APP,然后把电话号码生成二维码 或者条形码, 用手机扫描,这样,就不用担心会输入错误电话号码了。 在下面是实现的扫描二维码的功能