利用java生成二維碼工具類示例代碼
二維碼介紹
二維條形碼最早發(fā)明于日本,它是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息的,在代碼編制上巧妙地利用構(gòu)成計(jì)算機(jī)內(nèi)部邏輯基礎(chǔ)的“0”、“1”比特流的概念,使用若干個(gè)與二進(jìn)制相對應(yīng)的幾何形體來表示文字?jǐn)?shù)值信息,通過圖象輸入設(shè)備或光電掃描設(shè)備自動(dòng)識(shí)讀以實(shí)現(xiàn)信息自動(dòng)處理。
如下為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 編號(hào) * @param phone 手機(jī)號(hào) * @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<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); 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<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); 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("編號(hào):") .append(content.getString("number")) .append("\r\n") .append("手機(jī)號(hào)碼:") .append(content.getString("phone")); String returnText = sb.toString(); System.out.println(returnText); } catch (IOException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } } /** * * 生成二維碼輸出流 * 在jsp頁面中直接展示時(shí)使用 * 無須保存 即生成即展示 * @param response * @param number 編號(hào) * @param phone 手機(jī)號(hào) * @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<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); 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í)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
SpringBoot中的異常處理與參數(shù)校驗(yàn)的方法實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot中的異常處理與參數(shù)校驗(yàn)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04mybatis generator 配置 反向生成Entity簡單增刪改查(推薦)
這篇文章主要介紹了mybatis generator 配置 反向生成Entity簡單增刪改查(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12Java實(shí)戰(zhàn)玩具商城的前臺(tái)與后臺(tái)實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+JSP+SSM+Springboot+Jsp+maven+Mysql實(shí)現(xiàn)一個(gè)玩具商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁顯示
這篇文章主要為大家詳細(xì)介紹了SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁顯示的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06手把手教你寫Maven的archetype項(xiàng)目腳手架
本文主要介紹了Maven的archetype項(xiàng)目腳手架,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Spring Boot集成ShedLock分布式定時(shí)任務(wù)的實(shí)現(xiàn)示例
ShedLock確保您計(jì)劃的任務(wù)最多同時(shí)執(zhí)行一次。如果一個(gè)任務(wù)正在一個(gè)節(jié)點(diǎn)上執(zhí)行,則它會(huì)獲得一個(gè)鎖,該鎖將阻止從另一個(gè)節(jié)點(diǎn)(或線程)執(zhí)行同一任務(wù)。2021-05-05