1. 程式人生 > >zxing 二維碼、帶logo二維碼生成

zxing 二維碼、帶logo二維碼生成

<span style="font-size:18px;">普通二維碼生成</span>
<span style="font-size:18px;">public class ZxingEncoderUtil {

	/**
	 * 生成二維碼
	 * @param filePath       文字檔案路徑(文字需以UTF-8)
	 * @param imgFormate     指定生成的二維碼圖片的字尾名
	 * @param width          指定生成的二維碼圖片的寬度
	 * @param height		   指定生成的二維碼圖片的高度
	 */
    public void createTwoDimensionalCode(String filePath, String imgFormate, int width, int height) {
    	File file = new File(filePath);
    	StringBuffer contents = new StringBuffer("");
    	//讀取文字檔案內容到 contents
		try {
			InputStream is = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			try {
				String temp = br.readLine();
				while(temp != null) {
					contents.append(temp+"\r\n");
					temp = br.readLine();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//得到圖片該存放路徑
		String imgPath = file.getParent();
		//得到檔案該設定的名字
		String imgName = file.getName();
		int endIndex = imgName.lastIndexOf(".");
		imgName = imgName.substring(0, endIndex);
		
		File imageFile = new File(imgPath, imgName+"."+imgFormate);
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定糾錯等級
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 指定編碼格式
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents.toString(),BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToPath(bitMatrix, imgFormate, imageFile.toPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
    	
    	String filePath = args[0];   //文字檔案路徑(文字需以UTF-8)
    	String imgFormate = args[1];  //指定生成的二維碼圖片的字尾名
    	int width = Integer.parseInt(args[2]); //指定生成的二維碼圖片的寬度
    	int height = Integer.parseInt(args[3]); //指定生成的二維碼圖片的高度
    	new ZxingEncoderUtil().createTwoDimensionalCode(filePath, imgFormate, width, height);
    }
}</span>

帶logo二維碼

<span style="font-size:18px;">public class CreatQRCodeImg {

	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF; 
	
	/**
	 *  最終呼叫該方法生成二維碼圖片
	 * @param url 要生成二維碼的url
	 * @param imgPath 二維碼生成的絕對路徑
	 * @param logoPath 二維碼中間logo絕對地址
	 * @throws Exception
	 */
	public static void get2CodeImage(String url,String imgPath,String logoPath) throws Exception{
		String format = "png";
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); 
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
		BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);
		File qrcodeFile = new File(imgPath);  
		CreatQRCodeImg.writeToFile(bitMatrix, format, qrcodeFile, logoPath);  
	}
	
	/**
	 * 
	 * @param matrix 二維碼矩陣相關
	 * @param format 二維碼圖片格式
	 * @param file 二維碼圖片檔案
	 * @param logoPath logo路徑
	 * @throws IOException
	 */
	public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
		BufferedImage image = toBufferedImage(matrix);
		Graphics2D gs = image.createGraphics();
		
		//載入logo
		Image img = ImageIO.read(new File(logoPath));
		gs.drawImage(img, 125, 125, null);
		gs.dispose();
		img.flush();
		if(!ImageIO.write(image, format, file)){
			throw new IOException("Could not write an image of format " + format + " to " + file);  
		}
	}
	
	public static 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;	
	}

	
	public static void main(String[] args) {
		try {
			get2CodeImage("www.baidu.com","d:\\test\\logotest\\logocode.jpg","d:\\test\\logotest\\logo.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}</span>

zxing相關jar包下載地址

http://repo1.maven.org/maven2/com/google/zxing/javase/3.1.0/

相關推薦

zxing logo生成

<span style="font-size:18px;">普通二維碼生成</span><span style="font-size:18px;">public class ZxingEncoderUtil { /** * 生成二

QRCode 掃描掃描條形碼相簿獲取圖片後識別生成 Logo 支援微博微信 QQ 掃描樣式

目錄功能介紹根據之前公司的產品需求,參考 barcodescanner 改的,希望能幫助到有生成二維碼、掃描二維碼、識別圖片二維碼等需求的猿友。修改幅度較大,也就沒準備針對 barcodescanner 庫提交PR。 ZXing 生成可自定義顏色、帶 logo 的二維碼 ZX

java 使用zxing生成logo和文字說明的)

jar包maven地址        <dependency>             <groupId>com.google.zxing</groupId>             <artifactId>core<

ZXing實現生成logologo以及掃功能

先吐槽下,CSDN的新介面真是看得我不順眼,連HTML編輯器都用不了咯,回頭看以前寫的部落格,介面真是亂七八糟的,這一下子得有多少人不適應,搞什麼東東呢,而且上傳不了資源了,部落格的demo上傳不了咯,

基於PHP類庫PHPqrCode生成LOGO

PHPqrCode是一個PHP二維碼生成類庫,利用它可以輕鬆生成二維碼,官網提供了下載和多個演示demo,檢視地址:http://phpqrcode.sourceforge.net/。下載官網提供的類庫後,只需要使用phpqrcode.php就可以生成二維碼了,當然您的PHP環境必須開啟支援GD2。

thinkphp 使用phpqrcode生成logo生成海報

1-下載類庫 composer require aferrandini/phpqrcode -vvv 2-common的方法 //$text 文字的內容 //$logo logo圖片 function code($text,$logo){ //二維碼圖片儲存路徑

用phpqrcode生成logo, 需注意幾點,不注意是要進坑的哦.

先附上程式碼: include '../vendor/phpqrcode/phpqrcode.php'; $value = 'http://127.0.0.1/txw1958/'; //二維碼內容 $errorCorrectionLevel = 'L';//容錯級別

兩種JAVA生成logo的方法

  一、方法一       只使用一個zxing.jar生成二維碼,下載地址http://central.maven.org/maven2/com/google/zxing/core/        

使用python 生成 中間logo

推薦兩個庫:qrcode 和 python-qrcode。 qrcode 執行在 Python 3 版本上,它可以玩出很多花樣。例如能生成以下三種二維碼圖片:普通二維碼、帶圖片的藝術二維碼(黑白與彩色)、動態二維碼(黑白與彩色)。它比較適合直接用於生成二維碼圖

建立視覺logo的好看的

visual-qr-code 可以創建出精美的二維碼,與傳統的二維碼不同的是,設定的背景圖片或logo並不是只是在二維碼中心,背景圖片和logo都是半透明的,讓二維碼更好看 一、使用示例(詳細情況原始碼中的測試用例) 示例1: 測試程式碼 @Test public v

java生成logo

隨著微信在市場上的佔有率不對升高,二維碼開始逐漸進入人們的視野,掃碼支付,掃碼關注,掃碼開啟連線...究竟二維碼是個什麼東西我就不在這裡贅述了,關於這方面大家可以去上搜索引擎我在這裡就簡單介紹一下一個Java的生成二維碼工具,是Google提供的,zxing原始碼:packa

建議收藏備用:.net core使用QRCoder生成普通Logo詳細使用教程,原始碼已更新至開源模板

隨著網際網路越來越生活化,二維碼的使用越來越普遍,不論是掃碼支付還是掃碼關注引流,似乎我們總是離不開二維碼,那麼很多需要推廣的文章或社群想要自己的二維碼,那麼你是不是需要在網站直接提供給使用者呢?很多開發者就在網上百度解決方案,邊做邊踩坑,甚至很多人寫的開發案例都是截圖或者類庫引用都沒說清楚,在這個摸索的途中

滿叉樹完全叉樹最優叉樹(赫夫曼樹)叉排序樹叉判定樹

二叉排序樹(Binary Sort Tree)又稱二叉查詢樹。 它或者是一棵空樹;或者是具有下列性質的二叉樹: (1)若左子樹不空,則左子樹上所有結點的值均小於它的根結點的值; (2)若右子樹不空,則右子樹上所有結點的值均大於它的根結點的值; (3)左、右子樹也分別為二叉排序樹;

spring 各個版本的源jar包和源jar包下載地址

lease jar包下載 org pretty jar span code mave 3.2 spring各個版本的源碼jar包、文檔jar包和spring的jar包 http://maven.springframework.org/rele

Swagger結合mustache模板生成後臺接口代以及前後臺建模代

erl null enum div tco enc property 取模 javax 之前項目中使用的的thrift來建模,維護前後臺模型以及rest接口,前臺使用的是angular2; 但是使用thrift只能生成建模,後臺的rest接口的Controller文件還是

C# ZXing.Net生成識別生成Logo(一)

tree bit 字符串 單位 images j2se lba 支付 .net 一.ZXing.Net 源代碼地址:http://zxingnet.codeplex.com/ 也可以使用Nuget包管理,添加如圖: 說明:ZXing是一個開源Java類庫用於解析多種格式的

zxing生成logo

首先說下,QRCode是日本人開發的,ZXing是google開發,barcode4j也是老美開發的,barcode4j對一維條形碼處理的很好,而且支援的格式很多,當然也可以對二維碼進行處理,效果個人感覺沒有前兩種好;ZXing對j2me,j2se,還有Android等支援也

Zxing 生成logo 下面放文字

生成的二維碼圖片: package com.psd.utils; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOExc

java使用zxing生成logo圖片

專案原始碼:qrcode 主要需要zxing的core-3.2.1.jar以及javase-3.2.1.jar包。 但是有一個問題就是zxing.3.2.1版本是Java1.7編譯的,所以如果我們需要java1.6以及以下版本相容的話,可以使用如下程式碼: package

Java生成logo

angle orm new ops 步驟 path rotated ade mat 這個工具類主要實現了兩點功能: 1. 生成任意文鏈接的二維碼. 2. 在二維碼的中間加入圖像. 主要實現步驟: 第一步: 導入QR二維碼3.0 版本的core包和一張jpg圖片(logo)。