Android如何實(shí)現(xiàn)URL轉(zhuǎn)換成二維碼
二維碼已經(jīng)成為我們?nèi)粘I钪械囊粋€(gè)不可獲取的產(chǎn)物,火車票上,景區(qū)門票,超市付款等等都會(huì)有二維碼的身影。
本文將實(shí)現(xiàn)由URL轉(zhuǎn)換成二維碼的過(guò)程。
先看一下示例圖

從示例圖中我們可以清晰地看到,URL被轉(zhuǎn)換成了二維碼。
下面跟隨我來(lái)一起實(shí)現(xiàn)這個(gè)功能。
導(dǎo)入Google提供的開源庫(kù)
compile 'com.google.zxing:core:3.3.0'
來(lái)講解一下核心的部分:二維碼轉(zhuǎn)換
①生成二維碼Bitmap
public static boolean createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm, String filePath) {
try {
if (content == null || "".equals(content)) {
return false;
}
//配置參數(shù)
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//容錯(cuò)級(jí)別
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//設(shè)置空白邊距的寬度
hints.put(EncodeHintType.MARGIN, 2); //default is 4
// 圖像數(shù)據(jù)轉(zhuǎn)換,使用了矩陣轉(zhuǎn)換
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
int[] pixels = new int[widthPix * heightPix];
// 下面這里按照二維碼的算法,逐個(gè)生成二維碼的圖片,
// 兩個(gè)for循環(huán)是圖片橫列掃描的結(jié)果
for (int y = 0; y < heightPix; y++) {
for (int x = 0; x < widthPix; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * widthPix + x] = 0xff000000;
} else {
pixels[y * widthPix + x] = 0xffffffff;
}
}
}
// 生成二維碼圖片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
if (logoBm != null) {
bitmap = addLogo(bitmap, logoBm);
}
//必須使用compress方法將bitmap保存到文件中再進(jìn)行讀取。直接返回的bitmap是沒(méi)有任何壓縮的,內(nèi)存消耗巨大!
return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
} catch (WriterException | IOException e) {
e.printStackTrace();
}
return false;
}
②在二維碼中間添加Logo圖案
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (src == null) {
return null;
}
if (logo == null) {
return src;
}
//獲取圖片的寬高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
if (srcWidth == 0 || srcHeight == 0) {
return null;
}
if (logoWidth == 0 || logoHeight == 0) {
return src;
}
//logo大小為二維碼整體大小的1/5
float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
③創(chuàng)建二維碼文件存儲(chǔ)目錄
private static String getFileRoot(Context context) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File external = context.getExternalFilesDir(null);
if (external != null) {
return external.getAbsolutePath();
}
}
return context.getFilesDir().getAbsolutePath();
}
④創(chuàng)建數(shù)據(jù)庫(kù)工具類來(lái)存儲(chǔ)臨時(shí)數(shù)據(jù)
public class SPUtil {
private static final String CONFIG = "config";
/**
* 獲取SharedPreferences實(shí)例對(duì)象
*
* @param fileName
*/
private static SharedPreferences getSharedPreference(String fileName) {
return QRCodeApplication.getInstance().getSharedPreferences(fileName, Context.MODE_PRIVATE);
}
/**
* 保存一個(gè)String類型的值!
*/
public static void putString(String key, String value) {
SharedPreferences.Editor editor = getSharedPreference(CONFIG).edit();
editor.putString(key, value).apply();
}
/**
* 獲取String的value
*/
public static String getString(String key, String defValue) {
SharedPreferences sharedPreference = getSharedPreference(CONFIG);
return sharedPreference.getString(key, defValue);
}
}
⑤展示二維碼
public static void showThreadImage(final Activity mContext, final String text, final ImageView imageView, final int centerPhoto) {
String preContent = SPUtil.getString("share_code_content", "");
if (text.equals(preContent)) {
String preFilePath = SPUtil.getString("share_code_filePath", "");
imageView.setImageBitmap(BitmapFactory.decodeFile(preFilePath));
} else {
SPUtil.putString("share_code_content", text);
final String filePath = getFileRoot(mContext) + File.separator + "qr_" + System.currentTimeMillis() + ".jpg";
SPUtil.putString("share_code_filePath", filePath);
//二維碼圖片較大時(shí),生成圖片、保存文件的時(shí)間可能較長(zhǎng),因此放在新線程中
new Thread(new Runnable() {
@Override
public void run() {
boolean success = QRCodeUtil.createQRImage(text, 800, 800, BitmapFactory.decodeResource(mContext.getResources(), centerPhoto),
filePath);
if (success) {
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
}
});
}
}
}).start();
}
}
構(gòu)造一個(gè)輸入頁(yè)面的類,使用Bundle通過(guò)<key,value>傳值(后期會(huì)改為MVVM-DataBinding形式)
public class ContentActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etUrl;
private Button btnConvert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
initView();
}
private void initView() {
etUrl = (EditText) findViewById(R.id.et_url);
btnConvert = (Button) findViewById(R.id.btn_convert);
btnConvert.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_convert:
String str_url = "https://" + etUrl.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("url", str_url);
// 當(dāng)輸入框?yàn)榭諘r(shí),提示用戶
if (str_url.equals("https://")) {
Toast.makeText(getApplicationContext(), "輸入框不能為空", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(ContentActivity.this, MainActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
break;
default:
break;
}
}
}
將二維碼圖片展示在頁(yè)面上(后期會(huì)改為MVVM-DataBinding形式)
public class MainActivity extends AppCompatActivity {
private ImageView iv;
// private String url = "http://weibo.com/cnwutianhao";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str_url = getIntent().getExtras().getString("url");
iv = (ImageView) findViewById(R.id.iv_qrcode);
QRCodeUtil.showThreadImage(this, str_url, iv, R.mipmap.ic_launcher);
}
}
布局文件
①輸入頁(yè)面(后期會(huì)改為DataBinding形式)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <EditText android:id="@+id/et_url" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="100dp" android:hint="請(qǐng)輸入網(wǎng)址" android:inputType="textUri" /> <Button android:id="@+id/btn_convert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:text="轉(zhuǎn)換成二維碼" /> </RelativeLayout>
②二維碼展示頁(yè)面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tnnowu.android.qrcode.MainActivity"> <ImageView android:id="@+id/iv_qrcode" android:layout_width="220dp" android:layout_height="220dp" android:layout_centerInParent="true" android:layout_marginTop="40dp" android:background="#FFFFFF" /> </RelativeLayout>
源代碼已上傳至Github,https://github.com/cnwutianhao/QRCode
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
- Android上使用ZXing識(shí)別條形碼與二維碼的方法
- Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問(wèn)題
- 基于Android實(shí)現(xiàn)個(gè)性彩色好看的二維碼
- Android編程實(shí)現(xiàn)二維碼的生成與解析
- Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果
- iOS和Android用同一個(gè)二維碼實(shí)現(xiàn)跳轉(zhuǎn)下載鏈接的方法
- Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能
- Android基于google Zxing實(shí)現(xiàn)二維碼的生成
- Android 二維碼 生成和識(shí)別二維碼 附源碼下載
相關(guān)文章
Android實(shí)現(xiàn)日夜間模式的深入理解
相信Android的日間/夜間模式切換相信大家在平時(shí)使用 APP 的過(guò)程中都遇到過(guò),比如知乎、簡(jiǎn)書中就有相關(guān)的模式切換。實(shí)現(xiàn)日間/夜間模式切換的方案也有許多種,趁著今天有空來(lái)講一下日間/夜間模式切換的幾種實(shí)現(xiàn)方案,也可以做一個(gè)橫向的對(duì)比來(lái)看看哪種方案最好。2016-09-09
Android 跨進(jìn)程模擬按鍵(KeyEvent )實(shí)例詳解
這篇文章主要介紹了Android 跨進(jìn)程模擬按鍵(KeyEvent )實(shí)例詳解的相關(guān)資料,類似手機(jī)遙控器的需求就可以這么做,需要的朋友可以參考下2016-11-11
Android實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登錄注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
android 仿微信demo——注冊(cè)功能實(shí)現(xiàn)(服務(wù)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗【幱X(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android 中 SwipeLayout一個(gè)展示條目底層菜單的側(cè)滑控件源碼解析
這篇文章主要介紹了Android 中 SwipeLayout一個(gè)展示條目底層菜單的側(cè)滑控件源碼解析,需要的朋友可以參考下2016-12-12
Android實(shí)現(xiàn)一個(gè)絲滑的自動(dòng)輪播控件實(shí)例代碼
輪播圖對(duì)大家來(lái)說(shuō)應(yīng)該再熟悉不過(guò)了,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)一個(gè)絲滑的自動(dòng)輪播控件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

