当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现二维码扫描和生成的简单方法

Android实现二维码扫描和生成的简单方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

五月芙蓉,罗通拜帅,地狱神探2

这里简单介绍一下zxing库。zxing是一个开放源码的,用java实现的多种格式的1d/2d条码图像处理库,它包含了联系到其他语言的端口。zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。该项目可实现的条形码编码和解码。目前支持以下格式:upc-a,upc-e、ean-8,ean-13、39码、93码。zxing是个很经典的条码/二维码识别的开源类库,以前在功能机上,就有开发者使用j2me运用zxing了,不过要支持jsr-234规范(自动对焦)的手机才能发挥其威力。

zxinggithub地址

效果图:

这里写图片描述

主要实现步骤:

导入libzxing这个模块

这里写图片描述

zxing源代码很大,功能也很多,这里只是抽取了其中的一部分代码整合到了一起

扫描

在main_activity中添加一个button和一个textview 点击button后开始调照相机功能,扫描二维码
textview会显示扫描后的结果

<button
android:text="strat scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onclick="scan"/>
<textview
android:id="@+id/tv_showresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world!"/>

在activitymain中分别初始化这两个控件

private textview mtextview;
mtextview= (textview) this.findviewbyid(r.id.tv_showresult);
//扫描二维码
//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二维码生成网站
public void scan(view view) {
startactivityforresult(new intent(this, captureactivity.class),0);
}
@override
protected void onactivityresult(int requestcode, int resultcode, intent data) {
super.onactivityresult(requestcode, resultcode, data);
if (resultcode==result_ok){
bundle bundle = data.getextras();
if (bundle != null) {
string result=bundle.getstring("result");
mtextview.settext(result);
}
}
}

生成二维码

这里就把整个项目的xml文件都贴出来把,加上之前的扫描

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
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"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.hfs.zxingdemo.mainactivity">
<button
android:text="strat scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onclick="scan"/>
<textview
android:id="@+id/tv_showresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world!"/>
<edittext
android:id="@+id/et_text"
android:hint="imput"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onclick="make"
android:text="make qrcode"/>
<checkbox
android:id="@+id/cb_logo"
android:layout_width="wrap_content"
android:text="logo"
android:layout_height="wrap_content"/>
<imageview
android:id="@+id/img_shouw"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="@mipmap/ic_launcher"
android:layout_height="wrap_content"/>
</linearlayout>

mainactivity中代码

package com.example.hfs.zxingdemo;
import android.content.intent;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.hardware.camera2.capturerequest;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.checkbox;
import android.widget.edittext;
import android.widget.imageview;
import android.widget.textview;
import android.widget.toast;
import com.xys.libzxing.zxing.activity.captureactivity;
import com.xys.libzxing.zxing.encoding.encodingutils;
public class mainactivity extends appcompatactivity {
private textview mtextview;
private edittext medittext;
private imageview mimageview;
private checkbox mcheckbox;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
initview();
}
private void initview() {
mtextview= (textview) this.findviewbyid(r.id.tv_showresult);
medittext= (edittext) this.findviewbyid(r.id.et_text);
mimageview= (imageview) this.findviewbyid(r.id.img_shouw);
mcheckbox= (checkbox) this.findviewbyid(r.id.cb_logo);
}
//扫描二维码
//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二维码生成网站
public void scan(view view) {
startactivityforresult(new intent(this, captureactivity.class),0);
}
@override
protected void onactivityresult(int requestcode, int resultcode, intent data) {
super.onactivityresult(requestcode, resultcode, data);
if (resultcode==result_ok){
bundle bundle = data.getextras();
if (bundle != null) {
string result=bundle.getstring("result");
mtextview.settext(result);
}
}
}
//生成二维码 可以设置logo
public void make(view view) {
string input = medittext.gettext().tostring();
if (input.equals("")){
toast.maketext(this,"输入不能为空",toast.length_short).show();
}else{
bitmap qrcode = encodingutils.createqrcode(input, 500, 500,
mcheckbox.ischecked()? bitmapfactory.decoderesource(getresources(),r.mipmap.ic_launcher):null);//checkbox选中就设置logo
mimageview.setimagebitmap(qrcode);
}
}
}

好了 到这里就写完了

项目地址

以上所述是小编给大家介绍的android实现二维码扫描和生成的简单方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网