Android二维码的生成,解析以及扫描功能

<1>

布局只有2个按钮,实现生成二维码和解析二维码

    <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="create"
android:text="生成二維碼"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="parse"
android:text="解析二维码"
android:id="@+id/button2" />

2.

package myapplication.com.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource; import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter; import org.json.JSONObject; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void create(View v){
//构建二维码内容的结构的对象
QRCodeWriter writer=new QRCodeWriter();
//构造内存空间
String url="http://www.baidu.com"; int width=;
int height=;
try {
JSONObject value=new JSONObject();
value.put("url","http://www.baidu.com");
value.put("tel","");
value.put("account","");
url=value.toString();
BitMatrix matrix = writer.encode(url, BarcodeFormat.QR_CODE, width, height);
//数组
int ms[]=new int[width*height];
//给数组元素赋值
for(int y=;y<height;y++){
for(int x=;x<width;x++){
if (matrix.get(x,y)){
ms[y*width+x]=0xff000000;
}else{
ms[y*width+x]=0xffffffff;
}
}
}
//缓冲区
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(ms,,width,,,width,height);
FileOutputStream out=new FileOutputStream("/sdcard/abcd.png");
//压缩并保存
bitmap.compress(Bitmap.CompressFormat.PNG,,out);
} catch (Exception e) {
e.printStackTrace();
}
}
//解析二维码
public void parse(View v){
Bitmap map= BitmapFactory.decodeFile("/sdcard/abcd.png");
int width = map.getWidth();
int height = map.getHeight();
int ps[]=new int[width*height];
map.getPixels(ps,,width,,,width,height);
LuminanceSource src=new RGBLuminanceSource(width,height,ps);
Binarizer rizer=new HybridBinarizer(src);
BinaryBitmap bbmap=new BinaryBitmap(rizer);
QRCodeReader read=new QRCodeReader();
try {
Result result = read.decode(bbmap);
//根据获取到的文本内容进行下一步处理,这里是打开网址
String value=result.getText();
JSONObject msg=new JSONObject(value);
System.out.println("--"+msg.getString("account"));
Intent it=new Intent(Intent.ACTION_VIEW);
it.setData(Uri.parse(msg.getString("url")));
startActivity(it);
} catch (Exception e) {
e.printStackTrace();
}
}
}

3. 加入的jar包和权限

jar包地址:链接:http://pan.baidu.com/s/1crGxL8 密码:f1kp

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<2> 实现扫描二维码功能

1.加入权限

 <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>

2.ScannerActivity

package myapplication.com.myerweima;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import com.google.zxing.Result; import me.dm7.barcodescanner.zxing.ZXingScannerView; public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mZXingScannerView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mZXingScannerView = new ZXingScannerView(this); // 将ZXingScannerView作为布局
setContentView(mZXingScannerView);
} @Override
protected void onResume() {
super.onResume();
mZXingScannerView.setResultHandler(this); // 设置处理结果回调
mZXingScannerView.startCamera(); // 打开摄像头
} @Override
protected void onPause() {
super.onPause();
mZXingScannerView.stopCamera(); // 活动失去焦点的时候关闭摄像头
} @Override
public void handleResult(Result result) { // 实现回调接口,将数据回传并结束活动
Intent data = new Intent();
data.putExtra("text", result.getText());
setResult(RESULT_OK, data);
finish();
}
}

3. 主布局,一个button一个textview点击btn扫描,扫描后显示在textView上

 <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫描"
android:id="@+id/bt"
android:textColor="#000000"
/>
<TextView
android:id="@+id/text"
android:layout_marginTop="30dp"
android:layout_below="@+id/bt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#000000"
android:text="11"/>

4.

package myapplication.com.myerweima;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt= (Button) findViewById(R.id.bt);
textView= (TextView) findViewById(R.id.text);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanCode();
}
});
} public void scanCode(){
startActivityForResult(new Intent(this, ScannerActivity.class), 1);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
textView.setText(data.getStringExtra("text")); // 显示识别到的文字 }
}
}

5.gradle

  compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.zxing:zxing-parent:3.3.0'
compile 'me.dm7.barcodescanner:zxing:1.9'
上一篇:Android二维码扫描、生成


下一篇:js基础知识梳理(最简版)