简单二维码生成以及识别

1. 二维码

成都创新互联-专业网站定制、快速模板网站建设、高性价比莲都网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式莲都网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖莲都地区。费用合理售后完善,十余年实体公司更值得信赖。

本质:生成过程是将字符串转换为图形

识别:扫描图形转换为字符串


首先需要依赖lib_QRCode库(在附件中可以下载)

1.在代码中设置


package com.example.ex_templete;


import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.ImageView;

import android.widget.Toast;


import com.ericssonlabs.BarCodeTestActivity;

import com.google.zxing.WriterException;

import com.zxing.activity.CaptureActivity;

import com.zxing.encoding.EncodingHandler;


public class MainActivity extends Activity {

private ImageView mImageView;

public void scan(View v) {


//打开扫描界面扫描条形码或二维码

Intent openCameraIntent = new Intent(this, CaptureActivity.class);

startActivityForResult(openCameraIntent, 0);

}


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

//处理扫描结果(在界面上显示)

if (resultCode == RESULT_OK) {

Bundle bundle = data.getExtras();

String scanResult = bundle.getString("result");

//resultTextView.setText(scanResult);

}

}


public void onClick(View v) {


try {

String contentString = "http://www.baidu.com";

if (contentString != null && contentString.trim().length() > 0) {

// 根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)

Bitmap qrCodeBitmap = EncodingHandler.createQRCode(

contentString, 350);

mImageView.setImageBitmap(qrCodeBitmap);

} else {

Toast.makeText(this, "Text can not be empty",

Toast.LENGTH_SHORT).show();

}


} catch (WriterException e) {

e.printStackTrace();

}


}


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView = (ImageView) findViewById(R.id.p_w_picpathView1);

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}


2.xml中

    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"

    tools:context="com.example.ex_templete.MainActivity" >

   

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:onClick="onClick"

        android:text="生成二维码" />

   

        android:id="@+id/p_w_picpathView1"

        android:layout_width="230dp"

        android:layout_height="230dp"

        android:scaleType="fitXY"

        android:layout_alignLeft="@+id/button1"

        android:layout_below="@+id/button1"

        android:layout_marginTop="24dp"

        android:src="@drawable/ic_launcher" />

   

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/button1"

        android:layout_alignParentRight="true"

        android:layout_marginRight="30dp"

        android:onClick="scan"

        android:text="扫一扫" />

3.在文档中配置权限以及注册activity

    package="com.xinbo.demo"

    android:versionCode="1"

    android:versionName="1.0" >

   

        android:minSdkVersion="8"

        android:targetSdkVersion="18" />

   

   

   

   

   

   

   

   

   

   

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

       

            android:name="com.xinbo.demo.MainActivity"

            android:label="@string/app_name" >

           

               

               

           

       

       

            android:name="com.zxing.activity.CaptureActivity"

            android:configChanges="orientation|keyboardHidden"

            android:screenOrientation="portrait"

            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

            android:windowSoftInputMode="stateAlwaysHidden" >

       

   

附件:http://down.51cto.com/data/2365982

网页标题:简单二维码生成以及识别
分享URL:http://scyanting.com/article/pedhjo.html