1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      java二維碼碼代碼 java實(shí)現(xiàn)二維碼掃描

      如何用java生成二維碼

      package?common;

      創(chuàng)新互聯(lián) - 多線BGP機(jī)房,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽服務(wù)器托管,德陽服務(wù)器托管,遂寧服務(wù)器托管,綿陽服務(wù)器托管,四川云主機(jī),成都云主機(jī),西南云主機(jī),多線BGP機(jī)房,西南服務(wù)器托管,四川/成都大帶寬,成都機(jī)柜租用,四川老牌IDC服務(wù)商

      import?java.awt.Color;

      import?java.awt.Graphics2D;

      import?java.awt.Image;

      import?java.awt.image.BufferedImage;

      import?java.io.File;

      import?java.io.IOException;

      import?java.util.HashMap;

      import?java.util.Map;

      import?javax.imageio.ImageIO;

      import?jp.sourceforge.qrcode.QRCodeDecoder;

      import?jp.sourceforge.qrcode.exception.DecodingFailedException;

      import?com.google.zxing.BarcodeFormat;

      import?com.google.zxing.Binarizer;

      import?com.google.zxing.BinaryBitmap;

      import?com.google.zxing.EncodeHintType;

      import?com.google.zxing.LuminanceSource;

      import?com.google.zxing.MultiFormatReader;

      import?com.google.zxing.MultiFormatWriter;

      import?com.google.zxing.NotFoundException;

      import?com.google.zxing.WriterException;

      import?com.google.zxing.common.BitMatrix;

      import?com.google.zxing.common.HybridBinarizer;

      import?com.swetake.util.Qrcode;

      /**

      *?二維碼生成工具類

      *?@author?Cloud

      *?@data???2016-12-15

      *?QRCode

      */

      public?class?QRCodeUtil?{

      //二維碼顏色

      private?static?final?int?BLACK?=?0xFF000000;

      //二維碼顏色

      private?static?final?int?WHITE?=?0xFFFFFFFF;

      /**

      *?span?style="font-size:18px;font-weight:blod;"ZXing?方式生成二維碼/span

      *?@param?text????a?href="javascript:void();"二維碼內(nèi)容/a

      *?@param?width????二維碼寬

      *?@param?height????二維碼高

      *?@param?outPutPath????二維碼生成保存路徑

      *?@param?imageType????????二維碼生成格式

      */

      public?static?void?zxingCodeCreate(String?text,?int?width,?int?height,?String?outPutPath,?String?imageType){

      MapEncodeHintType,?String?his?=?new?HashMapEncodeHintType,?String();

      //設(shè)置編碼字符集

      his.put(EncodeHintType.CHARACTER_SET,?"utf-8");

      try?{

      //1、生成二維碼

      BitMatrix?encode?=?new?MultiFormatWriter().encode(text,?BarcodeFormat.QR_CODE,?width,?height,?his);

      //2、獲取二維碼寬高

      int?codeWidth?=?encode.getWidth();

      int?codeHeight?=?encode.getHeight();

      //3、將二維碼放入緩沖流

      BufferedImage?image?=?new?BufferedImage(codeWidth,?codeHeight,?BufferedImage.TYPE_INT_RGB);

      for?(int?i?=?0;?i??codeWidth;?i++)?{

      for?(int?j?=?0;?j??codeHeight;?j++)?{

      //4、循環(huán)將二維碼內(nèi)容定入圖片

      image.setRGB(i,?j,?encode.get(i,?j)???BLACK?:?WHITE);

      }

      }

      File?outPutImage?=?new?File(outPutPath);

      //如果圖片不存在創(chuàng)建圖片

      if(!outPutImage.exists())

      outPutImage.createNewFile();

      //5、將二維碼寫入圖片

      ImageIO.write(image,?imageType,?outPutImage);

      }?catch?(WriterException?e)?{

      e.printStackTrace();

      System.out.println("二維碼生成失敗");

      }?catch?(IOException?e)?{

      e.printStackTrace();

      System.out.println("生成二維碼圖片失敗");

      }

      }

      /**

      *?span?style="font-size:18px;font-weight:blod;"二維碼解析/span

      *?@param?analyzePath????二維碼路徑

      *?@return

      *?@throws?IOException

      */

      @SuppressWarnings({?"rawtypes",?"unchecked"?})

      public?static?Object?zxingCodeAnalyze(String?analyzePath)?throws?Exception{

      MultiFormatReader?formatReader?=?new?MultiFormatReader();

      Object?result?=?null;

      try?{

      File?file?=?new?File(analyzePath);

      if?(!file.exists())

      {

      return?"二維碼不存在";

      }

      BufferedImage?image?=?ImageIO.read(file);

      LuminanceSource?source?=?new?LuminanceSourceUtil(image);

      Binarizer?binarizer?=?new?HybridBinarizer(source);??

      BinaryBitmap?binaryBitmap?=?new?BinaryBitmap(binarizer);

      Map?hints?=?new?HashMap();

      hints.put(EncodeHintType.CHARACTER_SET,?"UTF-8");

      result?=?formatReader.decode(binaryBitmap,?hints);

      }?catch?(NotFoundException?e)?{

      e.printStackTrace();

      }??

      return?result;

      }

      /**

      *?span?style="font-size:18px;font-weight:blod;"QRCode?方式生成二維碼/span

      *?@param?content????二維碼內(nèi)容

      *?@param?imgPath????二維碼生成路徑

      *?@param?version????二維碼版本

      *?@param?isFlag????是否生成Logo圖片????為NULL不生成

      */

      public?static?void?QRCodeCreate(String?content,?String?imgPath,?int?version,?String?logoPath){

      try?{??

      Qrcode?qrcodeHandler?=?new?Qrcode();??

      //設(shè)置二維碼排錯(cuò)率,可選L(7%)?M(15%)?Q(25%)?H(30%),排錯(cuò)率越高可存儲(chǔ)的信息越少,但對二維碼清晰度的要求越小????

      qrcodeHandler.setQrcodeErrorCorrect('M');??

      //N代表數(shù)字,A代表字符a-Z,B代表其他字符??

      qrcodeHandler.setQrcodeEncodeMode('B');??

      //版本1為21*21矩陣,版本每增1,二維碼的兩個(gè)邊長都增4;所以版本7為45*45的矩陣;最高版本為是40,是177*177的矩陣??

      qrcodeHandler.setQrcodeVersion(version);

      //根據(jù)版本計(jì)算尺寸

      int?imgSize?=?67?+?12?*?(version?-?1)?;??

      byte[]?contentBytes?=?content.getBytes("gb2312");??

      BufferedImage?bufImg?=?new?BufferedImage(imgSize?,?imgSize?,BufferedImage.TYPE_INT_RGB);??

      Graphics2D?gs?=?bufImg.createGraphics();??

      gs.setBackground(Color.WHITE);??

      gs.clearRect(0,?0,?imgSize?,?imgSize);??

      //?設(shè)定圖像顏色??BLACK

      gs.setColor(Color.BLACK);

      //?設(shè)置偏移量?不設(shè)置可能導(dǎo)致解析出錯(cuò)??

      int?pixoff?=?2;

      //?輸出內(nèi)容??二維碼??

      if?(contentBytes.length??0??contentBytes.length??130)?{

      boolean[][]?codeOut?=?qrcodeHandler.calQrcode(contentBytes);

      for?(int?i?=?0;?i??codeOut.length;?i++)?{

      for?(int?j?=?0;?j??codeOut.length;?j++)?{

      if?(codeOut[j][i])?{??

      gs.fillRect(j?*?3?+?pixoff,?i?*?3?+?pixoff,?3,?3);

      }??

      }??

      }??

      }?else?{??

      System.err.println("QRCode?content?bytes?length?=?"?+?contentBytes.length?+?"?not?in?[?0,130?].?");??

      }

      /*?判斷是否需要添加logo圖片?*/

      if(logoPath?!=?null){

      File?icon?=?new?File(logoPath);

      if(icon.exists()){

      int?width_4?=?imgSize?/?4;

      int?width_8?=?width_4?/?2;

      int?height_4?=?imgSize?/?4;

      int?height_8?=?height_4?/?2;

      Image?img?=?ImageIO.read(icon);

      gs.drawImage(img,?width_4?+?width_8,?height_4?+?height_8,width_4,height_4,?null);

      gs.dispose();

      bufImg.flush();

      }else{

      System.out.println("Error:?login圖片還在在!");

      }

      }

      gs.dispose();

      bufImg.flush();

      //創(chuàng)建二維碼文件

      File?imgFile?=?new?File(imgPath);

      if(!imgFile.exists())

      imgFile.createNewFile();

      //根據(jù)生成圖片獲取圖片

      String?imgType?=?imgPath.substring(imgPath.lastIndexOf(".")?+?1,?imgPath.length());

      //?生成二維碼QRCode圖片??

      ImageIO.write(bufImg,?imgType,?imgFile);??

      }?catch?(Exception?e)?{??

      e.printStackTrace();??

      }

      }

      /**

      *?span?style="font-size:18px;font-weight:blod;"QRCode二維碼解析/span

      *?@param?codePath????二維碼路徑

      *?@return????解析結(jié)果

      */

      public?static?String?QRCodeAnalyze(String?codePath)?{

      File?imageFile?=?new?File(codePath);

      BufferedImage?bufImg?=?null;??

      String?decodedData?=?null;??

      try?{

      if(!imageFile.exists())

      return?"二維碼不存在";

      bufImg?=?ImageIO.read(imageFile);

      QRCodeDecoder?decoder?=?new?QRCodeDecoder();??

      decodedData?=?new?String(decoder.decode(new?ImageUtil(bufImg)),?"gb2312");??

      }?catch?(IOException?e)?{??

      System.out.println("Error:?"?+?e.getMessage());??

      e.printStackTrace();??

      }?catch?(DecodingFailedException?dfe)?{??

      System.out.println("Error:?"?+?dfe.getMessage());??

      dfe.printStackTrace();??

      }

      return?decodedData;

      }

      }

      3、最后貼測試代碼:

      package?test;

      import?java.awt.image.BufferedImage;

      import?java.io.InputStream;

      import?java.net.URL;

      import?javax.imageio.ImageIO;

      import?common.ImageUtil;

      import?common.QRCodeUtil;

      import?jp.sourceforge.qrcode.QRCodeDecoder;

      /**

      *?二維碼生成測試類

      *?@author?Cloud

      *?@data???2016-11-21

      *?QRCodeTest

      */

      public?class?QRCodeTest?{

      public?static?void?main(String[]?args)?throws?Exception?{

      /**

      *????QRcode?二維碼生成測試

      *????QRCodeUtil.QRCodeCreate("",?"E://qrcode.jpg",?15,?"E://icon.png");

      */

      /**

      *?????QRcode?二維碼解析測試

      *????String?qrcodeAnalyze?=?QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg");

      */

      /**

      *?ZXingCode?二維碼生成測試

      *?QRCodeUtil.zxingCodeCreate("",?300,?300,?"E://zxingcode.jpg",?"jpg");

      */

      /**

      *?ZxingCode?二維碼解析

      *????String?zxingAnalyze?=??QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString();

      */

      System.out.println("success");

      }

      }

      java中怎樣用代碼生成二維碼?

      參考代碼

      import java.io.*;

      import java.util.Date;

      import java.awt.*;

      import java.awt.image.*;

      import javax.imageio.*;

      public class QRCodeEncoderTest

      {

      /** Creates a new instance of QRCodeEncoderTest */

      public QRCodeEncoderTest()

      {

      }

      public static void create_image(String sms_info)throws Exception{

      try{

      qrcode testQrcode =new qrcode();

      testQrcode.setQrcodeErrorCorrect('M');

      testQrcode.setQrcodeEncodeMode('B');

      testQrcode.setQrcodeVersion(7);

      String testString = sms_info;

      byte[] d = testString.getBytes("gbk");

      System.out.println(d.length);

      //BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);

      BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_BYTE_BINARY);

      Graphics2D g = bi.createGraphics();

      g.setBackground(Color.WHITE);

      g.clearRect(0, 0, 98, 98);

      g.setColor(Color.BLACK);

      // 限制最大字節(jié)數(shù)為120

      if (d.length0 d.length 120){

      boolean[][] s = testQrcode.calQrcode(d);

      for (int i=0;is.length;i++){

      for (int j=0;js.length;j++){

      if (s[j][i]) {

      g.fillRect(j*2+3,i*2+3,2,2);

      }

      }

      }

      }

      g.dispose();

      bi.flush();

      File f = new File("D:\\QRCodeTest\\"+sms_info+".jpg");

      if(!f.exists()){

      f.createNewFile();

      }

      //創(chuàng)建圖片

      ImageIO.write(bi, "jpg", f);

      } // end try

      catch (Exception e) {

      e.printStackTrace();

      } // end catch

      }

      public static void main(String[] args) throws Exception {

      System.out.println(new Date());

      for(int i =1; i 100000; i ++){

      QRCodeEncoderTest.create_image(i+"");

      }

      System.out.println(new Date());

      } // end main

      }

      怎么使用java生成DataMatrix格式的二維碼?

      參考:

      import com.spire.barcode.BarCodeGenerator;

      import com.spire.barcode.BarCodeType;

      import com.spire.barcode.BarcodeSettings;

      import javax.imageio.ImageIO;

      import java.awt.*;

      import java.awt.image.BufferedImage;

      import java.io.File;

      public class CreateDataMatrix {

      public static void main(String[] args) throws Exception {

      ? //生成BarcodeSettings實(shí)例

      ? BarcodeSettings settings = new BarcodeSettings();

      ? //設(shè)置條形碼類型為DataMatrix

      ? settings.setType(BarCodeType.Data_Matrix);

      ? //設(shè)置條形碼模型寬度

      ? settings.setX(1.5f);

      ? //設(shè)置數(shù)據(jù)和顯示文本

      ? settings.setData("ABC 123456789ABC 123456789ABC 123456789");

      ? settings.setData2D("ABC 123456789ABC 123456789ABC 123456789");

      ? //創(chuàng)建BarCodeGenerator實(shí)例

      ? BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

      ? //根據(jù)settings生成圖像數(shù)據(jù),保存至BufferedImage實(shí)例

      ? BufferedImage bufferedImage = barCodeGenerator.generateImage();

      ? //保存為PNG圖片

      ? ImageIO.write(bufferedImage, "png", new File("DataMatrix.png"));

      ? System.out.println("Complete!");

      }

      }

      用到了spire.barcode for java庫

      Java如何用代碼生成二維碼

      引用spire.barcode.jar包

      //創(chuàng)建BarcodeSettings對象

      BarcodeSettings?settings?=?new?BarcodeSettings();

      //設(shè)置條碼類型為

      QR二維碼settings.setType(BarCodeType.QR_Code);???????

      //設(shè)置二維碼數(shù)據(jù)

      settings.setData("Hello?123456789");

      //設(shè)置二維碼顯示數(shù)據(jù)

      settings.setData2D("Hello?123456789");?????

      //設(shè)置數(shù)據(jù)類型

      settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);

      //設(shè)置二維碼模型寬度

      settings.setX(1.0f);

      //設(shè)置二維碼糾錯(cuò)級(jí)別settings.setQRCodeECL(QRCodeECL.H);

      //創(chuàng)建BarCodeGenerator實(shí)例

      BarCodeGenerator?barCodeGenerator?=?new?BarCodeGenerator(settings);

      //根據(jù)settings生成圖像數(shù)據(jù),保存至BufferedImage

      BufferedImage?bufferedImage?=?barCodeGenerator.generateImage();

      //將圖片數(shù)據(jù)保存為PNG格式

      ImageIO.write(bufferedImage,?"png",?new?File("QRCode.png"));


      本文標(biāo)題:java二維碼碼代碼 java實(shí)現(xiàn)二維碼掃描
      分享網(wǎng)址:http://ef60e0e.cn/article/ddojscg.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        佛山市| 桦南县| 辽宁省| 东山县| 桐乡市| 江北区| 北票市| 获嘉县| 白水县| 潞西市| 司法| 嵩明县| 汤原县| 枞阳县| 宽城| 博罗县| 龙江县| 资溪县| 边坝县| 土默特左旗| 巴林左旗| 内江市| 沭阳县| 扶风县| 体育| 湘潭市| 沅江市| 怀远县| 南陵县| 双牌县| 宁陕县| 镇赉县| 新郑市| 阿坝县| 望都县| 慈溪市| 通州区| 衡阳县| 康保县| 教育| 务川|