Android二維碼創(chuàng)建實(shí)例
Android二維碼之創(chuàng)建
實(shí)現(xiàn)效果圖:

1.Android 有自帶的jar包可以生成二維碼core-3.0.0.jar,其中的com.google.zxing包
2.寫(xiě)一個(gè)二維碼生成的工具類(lèi),網(wǎng)上搜的話應(yīng)該一大堆。
實(shí)例代碼:
package com.example.administrator.twocodedemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.view.Gravity;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
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.QRCodeWriter;
import java.util.Hashtable;
/**
*
* 生成條形碼和二維碼的工具
*/
public class ZXingUtils {
/**
* 生成二維碼 要轉(zhuǎn)換的地址或字符串,可以是中文
*
* @param url
* @param width
* @param height
* @return
*/
public static Bitmap createQRImage(String url, final int width, final int height) {
try {
// 判斷URL合法性
if (url == null || "".equals(url) || url.length() < 1) {
return null;
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 圖像數(shù)據(jù)轉(zhuǎn)換,使用了矩陣轉(zhuǎn)換
BitMatrix bitMatrix = new QRCodeWriter().encode(url,
BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
// 下面這里按照二維碼的算法,逐個(gè)生成二維碼的圖片,
// 兩個(gè)for循環(huán)是圖片橫列掃描的結(jié)果
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
} else {
pixels[y * width + x] = 0xffffffff;
}
}
}
// 生成二維碼圖片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 生成條形碼
*
* @param context
* @param contents
* 需要生成的內(nèi)容
* @param desiredWidth
* 生成條形碼的寬帶
* @param desiredHeight
* 生成條形碼的高度
* @param displayCode
* 是否在條形碼下方顯示內(nèi)容
* @return
*/
public static Bitmap creatBarcode(Context context, String contents,
int desiredWidth, int desiredHeight, boolean displayCode) {
Bitmap ruseltBitmap = null;
/**
* 圖片兩端所保留的空白的寬度
*/
int marginW = 20;
/**
* 條形碼的編碼類(lèi)型
*/
BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
if (displayCode) {
Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2
* marginW, desiredHeight, context);
ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF(
0, desiredHeight));
} else {
ruseltBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
}
return ruseltBitmap;
}
/**
* 生成條形碼的Bitmap
*
* @param contents
* 需要生成的內(nèi)容
* @param format
* 編碼格式
* @param desiredWidth
* @param desiredHeight
* @return
* @throws WriterException
*/
protected static Bitmap encodeAsBitmap(String contents,
BarcodeFormat format, int desiredWidth, int desiredHeight) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = null;
try {
result = writer.encode(contents, format, desiredWidth,
desiredHeight, null);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 生成顯示編碼的Bitmap
*
* @param contents
* @param width
* @param height
* @param context
* @return
*/
protected static Bitmap creatCodeBitmap(String contents, int width,
int height, Context context) {
TextView tv = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setText(contents);
tv.setHeight(height);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setWidth(width);
tv.setDrawingCacheEnabled(true);
tv.setTextColor(Color.BLACK);
tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
tv.buildDrawingCache();
Bitmap bitmapCode = tv.getDrawingCache();
return bitmapCode;
}
/**
* 將兩個(gè)Bitmap合并成一個(gè)
*
* @param first
* @param second
* @param fromPoint
* 第二個(gè)Bitmap開(kāi)始繪制的起始位置(相對(duì)于第一個(gè)Bitmap)
* @return
*/
protected static Bitmap mixtureBitmap(Bitmap first, Bitmap second,
PointF fromPoint) {
if (first == null || second == null || fromPoint == null) {
return null;
}
int marginW = 20;
Bitmap newBitmap = Bitmap.createBitmap(
first.getWidth() + second.getWidth() + marginW,
first.getHeight() + second.getHeight(), Config.ARGB_4444);
Canvas cv = new Canvas(newBitmap);
cv.drawBitmap(first, marginW, 0, null);
cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newBitmap;
}
}
ZXingUtils
3.MainActivity
@OnClick({R.id.btn_create, R.id.iv_two_code})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_create:
String url = etUrl.getText().toString().trim();
Bitmap bitmap = ZXingUtils.createQRImage(url, ivTwoCode.getWidth(), ivTwoCode.getHeight());
ivTwoCode.setImageBitmap(bitmap);
例如:
String company=etCompany.getText().toString().trim() ;
String phone =etPhone .getText().toString().trim() ;
String email = etEmail.getText().toString().trim() ;
String web = etWeb.getText().toString().trim() ;
//二維碼中包含的文本信息
String contents= "BEGIN:VCARD\nVERSION:3.0\nORG:"+company+"\nTEL:"+phone+"\nURL:"+web+"\nEMAIL:"+email+"\nEND:VCARD";
try {
//調(diào)用方法createCode生成二維碼
Bitmap bm=createCode(contents, logo, BarcodeFormat.QR_CODE);
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android從圖片獲取二維碼的方法
- Android中g(shù)oogle Zxing實(shí)現(xiàn)二維碼與條形碼掃描
- Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描
- Android中使用ZXing生成二維碼(支持添加Logo圖案)
- Android利用ZXing掃描二維碼的實(shí)例代碼解析
- Android項(xiàng)目實(shí)戰(zhàn)(二十八):使用Zxing實(shí)現(xiàn)二維碼及優(yōu)化實(shí)例
- Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
- Android掃描二維碼時(shí)出現(xiàn)用戶禁止權(quán)限報(bào)錯(cuò)問(wèn)題解決辦法
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android Scroll實(shí)現(xiàn)彈性滑動(dòng)_列表下拉彈性滑動(dòng)的示例代碼
下面小編就為大家分享一篇Android Scroll實(shí)現(xiàn)彈性滑動(dòng)_列表下拉彈性滑動(dòng)的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android學(xué)習(xí)筆記之藍(lán)牙功能
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)筆記之藍(lán)牙功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android進(jìn)階教程之ViewGroup自定義布局
這篇文章主要給大家介紹了關(guān)于Android進(jìn)階教程之ViewGroup自定義布局的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
RxJava2.x實(shí)現(xiàn)定時(shí)器的實(shí)例代碼
本篇文章主要介紹了RxJava2.x實(shí)現(xiàn)定時(shí)器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Android中AndroidStudio&Kotlin安裝到運(yùn)行過(guò)程及常見(jiàn)問(wèn)題匯總
這篇文章主要介紹了Android(AndroidStudio&Kotlin)安裝到運(yùn)行過(guò)程及常見(jiàn)問(wèn)題匯總,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android自定義View實(shí)現(xiàn)抖音飄動(dòng)紅心效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)抖音飄動(dòng)紅心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android自定義View實(shí)現(xiàn)拼圖小游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)拼圖小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android開(kāi)發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為
在項(xiàng)目中,一進(jìn)入一個(gè)頁(yè)面, EditText默認(rèn)就會(huì)自動(dòng)獲取焦點(diǎn),很是郁悶,Android 如何讓EditText不自動(dòng)獲取焦點(diǎn)?于是搜集整理一番,曬出來(lái)和大家分享,希望對(duì)你們有所幫助2012-12-12

