以下是旧版本,这个是新版本的zxing,来自 朽木_不折 凯文-杰 博客(第一个人的博客功能就够用)。
第一步: 导入依赖:
compile project(':libzxing')
compile project(path: ':libzxing')
第二步: 导入module依赖包:
这个就是刚才所说的android的demo,新建一个android项目,将这个module导入工程并命名为libzxing。
------------------MainActivity------------------
package com.example.earl.myerwenma;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final int REQUESTCODE = 0;//请求吗
private final int RESULTCODE = 1;//结果码
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
Button create = (Button) findViewById(R.id.create);
final EditText content = (EditText) findViewById(R.id.content);
final ImageView bit = (ImageView) findViewById(R.id.bit);
textView = (TextView) findViewById(R.id.result_0);
//开始扫描按钮
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), REQUESTCODE);
}
});
//生成二维码按钮
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content_string = content.getText().toString();
Log.d(TAG, "onClick() returned: " + "取到的内容---" + content_string);
Bitmap bitmap = EncodingUtils.createQRCode(content_string, 200, 200, null);
bit.setImageBitmap(bitmap);
}
});
}
//接收返回结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult() returned: " + "接收结果.....");
if (resultCode == RESULT_OK) {
Log.d(TAG, "onActivityResult() returned: " + "返回成功");
Bundle bundle = data.getExtras();
String result = bundle.getString("result");
textView.setText(result);
Log.d(TAG, "----" + result + "---");
}
}
}
-------------------activity_main.xml--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始扫描"
/>
<TextView
android:id="@+id/result"
android:text="扫描结果是:"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result_0"
android:text=""
android:textSize="35sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/create"
android:text="生成二维码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/bit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>