最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
利用java生成二維碼工具類示例代碼

二維碼介紹

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計,常德網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:常德等地區(qū)。常德做網(wǎng)站價格咨詢:13518219792

二維條形碼最早發(fā)明于日本,它是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號信息的,在代碼編制上巧妙地利用構(gòu)成計算機內(nèi)部邏輯基礎(chǔ)的“0”、“1”比特流的概念,使用若干個與二進(jìn)制相對應(yīng)的幾何形體來表示文字?jǐn)?shù)值信息,通過圖象輸入設(shè)備或光電掃描設(shè)備自動識讀以實現(xiàn)信息自動處理。

利用java生成二維碼工具類示例代碼

如下為java生成二維碼工具類,可以選擇生成文件,或者直接在頁面生成,話不多說了,來一起看看詳細(xì)的示例代碼吧。

示例代碼

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.FileSystems; 
import java.util.HashMap; 
import java.util.Map; 
 
import javax.imageio.ImageIO; 
import javax.servlet.http.HttpServletResponse; 
 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
 
import com.alibaba.fastjson.JSONObject; 
import com.google.zxing.BarcodeFormat; 
import com.google.zxing.Binarizer; 
import com.google.zxing.BinaryBitmap; 
import com.google.zxing.DecodeHintType; 
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.Result; 
import com.google.zxing.WriterException; 
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 
import com.google.zxing.client.j2se.MatrixToImageWriter; 
import com.google.zxing.common.BitMatrix; 
import com.google.zxing.common.HybridBinarizer; 
 
/** 
 * 
 * 二維碼工具類 
 * @see [相關(guān)類/方法] 
 * @since [產(chǎn)品/模塊版本] 
 */ 
public class QRCodeUtil 
{ 
  
 private static final Logger log = LoggerFactory.getLogger(QRCodeUtil.class); 
  
  
 /** 
  * 
  * 生成二維碼文件測試 
  * @param filePath 文件路徑 
  * @param fileName 文件名 
  * @param number 編號 
  * @param phone 手機號 
  * @see [類、類#方法、類#成員] 
  */ 
 public static void generatEncodeTest(String filePath, String fileName, String number, String phone) 
 { 
   
  int width = 200; // 圖像寬度 
  int height = 200; // 圖像高度 
  String format = "png";// 圖像類型 
   
  JSONObject json = new JSONObject(); 
  json.put("number",number); 
  json.put("phone", phone); 
  String content = json.toJSONString();// 內(nèi)容 
   
  Map hints = new HashMap(); 
  hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
   
  try 
  { 
   BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩陣 
   String path = FileSystems.getDefault().getPath(filePath, fileName).toString(); 
   File file = new File(path); 
   MatrixToImageWriter.writeToFile(bitMatrix, format, file);// 輸出圖像 
   System.out.println("二維碼輸出成功"); 
   System.out.println("圖片地址:" + filePath + fileName); 
   System.out.println("---------------------------"); 
  } 
  catch (WriterException e) 
  { 
   e.printStackTrace(); 
   System.out.println("二維碼輸出異常"); 
  } 
  catch (IOException e) 
  { 
   e.printStackTrace(); 
   System.out.println("二維碼輸出異常"); 
  } 
 } 
  
 /** 
  * 
  * 解析二維碼內(nèi)容測試 
  * @param filePath 二維碼絕對路徑 
  * @see [類、類#方法、類#成員] 
  */ 
 public static void parseDecodeTest(String filePath) 
 { 
  BufferedImage image; 
  try 
  { 
   image = ImageIO.read(new File(filePath)); 
   LuminanceSource source = new BufferedImageLuminanceSource(image); 
   Binarizer binarizer = new HybridBinarizer(source); 
   BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); 
    
   Map hints = new HashMap(); 
   hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); 
    
   Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 對圖像進(jìn)行解碼 
   JSONObject content = JSONObject.parseObject(result.getText()); 
    
   StringBuffer sb = new StringBuffer(); 
   sb.append("編號:") 
    .append(content.getString("number")) 
    .append("\r\n") 
    .append("手機號碼:") 
    .append(content.getString("phone")); 
   String returnText = sb.toString(); 
   System.out.println(returnText); 
  } 
  catch (IOException e) 
  { 
   e.printStackTrace(); 
  } 
  catch (NotFoundException e) 
  { 
   e.printStackTrace(); 
  } 
 } 
  
  
 /** 
  * 
  * 生成二維碼輸出流 
  *  在jsp頁面中直接展示時使用 
  *  無須保存 即生成即展示 
  * @param response 
  * @param number 編號 
  * @param phone 手機號 
  * @see [類、類#方法、類#成員] 
  */ 
 public static void generatEncode(HttpServletResponse response, String number, String phone) 
 { 
  JSONObject json = new JSONObject(); 
  json.put("number",number); 
  json.put("phone", phone); 
  String content = json.toJSONString();// 內(nèi)容 
   
  int width = 200; // 圖像寬度 
  int height = 200; // 圖像高度 
  String format = "png";// 圖像類型 
   
  Map hints = new HashMap(); 
  hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
   
  try 
  { 
    
   BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩陣 
   MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());// 輸出圖像 
   log.info("二維碼輸出成功"); 
  } 
  catch (WriterException e) 
  { 
   e.printStackTrace(); 
   log.error("二維碼輸出異常"); 
  } 
  catch (IOException e) 
  { 
   e.printStackTrace(); 
   log.error("二維碼輸出異常"); 
  } 
 } 
  
 public static void main(String[] args) 
 { 
  generatEncodeTest("D:\\","zxing.png","001","13019931996"); 
  parseDecodeTest("D:\\zxing.png"); 
 } 
} 

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。


本文標(biāo)題:利用java生成二維碼工具類示例代碼
本文鏈接:http://fisionsoft.com.cn/article/jsdoso.html