<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 二維碼 1. 在代碼編制上巧妙地利用構成計算機內部邏輯基礎的“0”、“1”比特流的概念,使用若干個與二進制相對應的幾何形體來表示文字數值信息,通過圖象輸入設備或光電掃描設備自動識讀以實現信息自動處理。 2. 在許多種類的二維條碼中,常用的碼制有: Data Matrix,MaxiCode, Aztec,QR Code, Vericode,PDF417,Ultracode,Code 49,Code 16K等, 3. QR code碼是1994年由日本DW公司發明。QR來自英文「Quick Response」的縮寫,即快速反應的意思,源自發明者希望QR碼可讓其內容快速被解碼。QR碼最常見于日本、韓國;并為目前日本最流行的二維空間條碼。但二維碼的安全性也正備受挑戰,帶有惡意軟件和病毒正成為二維碼普及道路上的絆腳石。發展與防范二維碼的濫用正成為一個亟待解決的問題。 ![](https://box.kancloud.cn/dea6295e90c16a0080b06c0ae6ae5b27_1229x785.png) 糾錯能力越高,二維碼存儲的信息就越少,對二維碼的清晰度要求就越低。 ![](https://box.kancloud.cn/dbe41ebf7ea5fedc8e2e9417885407db_1023x632.png) ## 1. 生成二維碼 ### 1.1 直接用MatrixToImageWriter寫入文件或者流 ![](https://box.kancloud.cn/db6de5207b09c3da1f42e7bf5f16a4a4_860x447.png) * 有問題的 * 依賴 ~~~ <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> ~~~ ### 1.2 ~~~ package net.aexit.galaxy.earth.mediashare.qrcode; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Base64; import java.util.HashMap; /** * Created by dailin on 2017/11/30. * 生成二維碼工具類 */ public class QrGenerator { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private int width = 300; //二維碼寬度 private int height = 300;//二維碼高度 private String format = "png"; // 二維碼圖片保存格式 private String contents = ""; //掃描二維碼時產生的內容 /** * 生成BitMatrix二維碼 * @return BitMatrix */ public BitMatrix CreatQRCode() throws WriterException { //定義二維碼的參數 HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//設置二維碼的容錯等級 hints.put(EncodeHintType.MARGIN, 2);//邊距 return new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); } public BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 將生產的二維碼寫入圖片文件 * @param file * @throws IOException */ public void writeToFile(File file) throws IOException, WriterException { BitMatrix matrix = CreatQRCode(); BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } /** * 將生成的二維碼以二進制數組的形式返回 */ public byte[] writeToBytes() throws WriterException, IOException { BitMatrix matrix = CreatQRCode(); byte[] bytes = null; try(ByteArrayOutputStream byteArray = new ByteArrayOutputStream()){ BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, byteArray)) { throw new IOException("faild to write an image to bytes"); } bytes = byteArray.toByteArray(); } return bytes; } /** * 將生成的二維碼以base64編碼的形式返回 */ public String writeToBase64() throws WriterException, IOException { BitMatrix matrix = CreatQRCode(); String base64String=null; try(ByteArrayOutputStream byteArray = new ByteArrayOutputStream()){ BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, byteArray)) { throw new IOException("faild to write an image to bytes"); } byte[] bytes = byteArray.toByteArray(); base64String = Base64.getEncoder().encodeToString(bytes); } return base64String; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } } ~~~ 2. 實體類 ~~~ package JavaTest.erweima; import javax.security.auth.Subject; /** * Created by dailin on 2017/11/30. */ public class StudentInfo { private String name ; private String IdCard ; private String CarType ; private String number; private int Subject_One_grade; private int Subject_One_total; private int Subject_Two_grade; private int Subject_Two_total; private int Subject_Three_grade; private int Subject_Three_total; private int Subject_Four_grade; private int Subject_Four_total; private Boolean IsGraduate; public Boolean getGraduate() { return IsGraduate; } public void setGraduate(Boolean graduate) { IsGraduate = graduate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIdCard() { return IdCard; } public void setIdCard(String idCard) { IdCard = idCard; } public String getCarType() { return CarType; } public void setCarType(String carType) { CarType = carType; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public int getSubject_One_grade() { return Subject_One_grade; } public void setSubject_One_grade(int subject_One_grade) { Subject_One_grade = subject_One_grade; } public int getSubject_One_total() { return Subject_One_total; } public void setSubject_One_total(int subject_One_total) { Subject_One_total = subject_One_total; } public int getSubject_Two_grade() { return Subject_Two_grade; } public void setSubject_Two_grade(int subject_Two_grade) { Subject_Two_grade = subject_Two_grade; } public int getSubject_Two_total() { return Subject_Two_total; } public void setSubject_Two_total(int subject_Two_total) { Subject_Two_total = subject_Two_total; } public int getSubject_Three_grade() { return Subject_Three_grade; } public void setSubject_Three_grade(int subject_Three_grade) { Subject_Three_grade = subject_Three_grade; } public int getSubject_Three_total() { return Subject_Three_total; } public void setSubject_Three_total(int subject_Three_total) { Subject_Three_total = subject_Three_total; } public int getSubject_Four_grade() { return Subject_Four_grade; } public void setSubject_Four_grade(int subject_Four_grade) { Subject_Four_grade = subject_Four_grade; } public int getSubject_Four_total() { return Subject_Four_total; } public void setSubject_Four_total(int subject_Four_total) { Subject_Four_total = subject_Four_total; } @Override public String toString() { String graduation = "未結業"; String NEW_LINE = System.getProperty("line.separator"); if (IsGraduate==true) graduation = "結業"; return "學員姓名:" + name + NEW_LINE + "學員身份證號:" + IdCard + NEW_LINE + "學員車型:" + CarType + NEW_LINE + "學員編號:" + number + NEW_LINE + "科目一:" + Subject_One_grade + "/" + Subject_One_total + NEW_LINE + "科目二:" + Subject_Two_grade + "/" + Subject_Two_total + NEW_LINE + "科目三:" + Subject_Three_grade + "/" + Subject_Three_total + NEW_LINE + "科目四:" + Subject_Four_grade + "/" + Subject_Four_total + NEW_LINE + "是否結業:" + graduation; } } ~~~ 3. 測試類 ~~~ package JavaTest.erweima; import com.google.zxing.common.BitMatrix; import java.io.*; /** * Created by dailin on 2017/11/30. */ public class test { public static void main(String[] args) { StudentInfo student = new StudentInfo(); student.setName("代林"); student.setIdCard("220122199108105513"); student.setCarType("大奔"); student.setNumber("123"); student.setSubject_One_grade(25); student.setSubject_One_total(100); student.setSubject_Two_grade(25); student.setSubject_Two_total(100); student.setSubject_Three_grade(25); student.setSubject_Three_total(100); student.setSubject_Four_grade(25); student.setSubject_Four_total(100); student.setGraduate(true); String str = student.toString(); File file = new File("C:\\Users\\Administrator\\Desktop\\dialin.png"); ErWeiMa erweima = new ErWeiMa(); erweima.setContents(str); try { erweima.writeToFile(file); // 寫成圖片文件 byte[] bytes = erweima.writeToStream(); OutputStream baos2 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\aixin.png"); baos2.write(bytes); } catch (Exception e) { e.printStackTrace(); } } } ~~~ ## 改正后 1. 底層工具類要拋出具體的異常,不要try catch攔截,否則上層無法捕捉錯誤信息 2. 沒關閉流 ~~~ /** * 生成BitMatrix二維碼 * @return BitMatrix */ public BitMatrix CreatQRCode() throws WriterException { //定義二維碼的參數 HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//設置二維碼的容錯等級 hints.put(EncodeHintType.MARGIN, 2);//邊距 return new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); } public BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 將生產的二維碼寫入圖片文件 * @param file * @throws IOException */ public void writeToFile(File file) throws IOException, WriterException { BitMatrix matrix = CreatQRCode(); BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } /** * 將生成的二維碼以二進制數組的形式返回 */ public byte[] writeToBytes() throws WriterException, IOException { BitMatrix matrix = CreatQRCode(); byte[] bytes = null; // 這么寫,完事后自動關閉流 try(ByteArrayOutputStream byteArray = new ByteArrayOutputStream()){ BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, byteArray)) { throw new IOException("faild to write an image to bytes"); } bytes = byteArray.toByteArray(); } return bytes; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看