1. 快速集成扫码二维码,识别二维码功能。经过加工处理优化的,直接返回扫码结果字符串和图片的bitmap数据。
直接上代码:
package com.xinxinchelian.myscanzxing;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.example.scanzxing.zxing.android.CaptureActivity;
import com.example.scanzxing.zxing.common.Constantes;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* @Author: rhj
* @CreateDate: 2020/12/14 21:26
*/
public class MainActivity extends AppCompatActivity {
private AppCompatActivity mActivity;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
mActivity = this;
initView();
ScreenUtils(getWindow());
}
public static void ScreenUtils(Window window) {
if (window == null){
return;
}
window.setFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//---------------------------------------------------------------------
}
private void initView() {
findViewById(R.id.test_qr_scan_btn_TV).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//动态权限申请
if (ContextCompat.checkSelfPermission( mActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions( mActivity, new String[]{Manifest.permission.CAMERA}, 1);
} else {
goScan();
}
}
});
}
/**
* 跳转到扫码界面扫码
*/
private void goScan(){
Intent intent = new Intent(mActivity , CaptureActivity.class);
startActivityForResult(intent, Constantes.REQUEST_CODE_SCAN );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 扫描二维码/条码回传
if (requestCode == Constantes.REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
if (data != null) {
//返回的文本内容
String content = data.getStringExtra(Constantes.CODED_CONTENT);
Bitmap bitmap = data.getParcelableExtra(Constantes.CODED_BITMAP);
//返回的BitMap图像
// Bitmap bitmap = data.getParcelableExtra(DECODED_BITMAP_KEY);
// tv_scanResult.setText("你扫描到的内容是:" + content);
Toast.makeText(getApplicationContext(), "扫描到的内容是:" + content , Toast.LENGTH_LONG).show();
Log.e("扫描到的内容是" , "扫描到的内容是:" + content);
if (!TextUtils.isEmpty(content)){
TextView plateNoTV = findViewById(R.id.test_qr_plateNo_TV);
plateNoTV.setText("扫描结果: " + content);
TextView plateNoTV2 = findViewById(R.id.test_qr_plateNo_TV2);
plateNoTV2.setText("扫描结果反转义: " + testDecode(content));
if (content.contains("plateNo=")){
plateNoTV2.setText("扫描结果反转义: " + testDecode(content.substring(content.indexOf("plateNo=") + 8)));
}
}
if (bitmap != null){
ImageView textBitmapIMG = findViewById(R.id.test_Bitmap_IMG);
textBitmapIMG.setVisibility(View.VISIBLE);
textBitmapIMG.setImageBitmap(bitmap);
} else {
Log.e("扫描到的内容是" , "扫描到的内容是:bitmap = null" );
}
}
}
}
public String testEncode(String url){
// String url="http://192.168.0.19:8888/cas/login";
String urlEncodee= URLEncoder.encode(url);
System.out.println(urlEncodee);
return urlEncodee;
}
public String testDecode(String url){
// String url="http%3A%2F%2F192.168.0.19%3A8888%2Fcas%2Flogin";
String urlDecode= URLDecoder.decode(url);
System.out.println(urlDecode);
return urlDecode;
}
}
需要的权限 AndroidManifest.xml:
2. 导入aar包 scanzxinglibrary-release.aar(和jar功效一样)
3. 扫码效果,
demo下载地址:
https://download.csdn.net/download/renhaijuniryfe/19360939
https://download.csdn.net/download/renhaijuniryfe/19360983
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)