项目导入library或者gradle添加依赖;
------------------------------------------------
MainActivity:
package com.dash.a06_qr_code;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.dash.zxinglibrary.activity.CaptureActivity;
import com.dash.zxinglibrary.activity.CodeUtils;
import com.dash.zxinglibrary.util.ImageUtil;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1001;
private static final int REQUEST_IMAGE = 1002;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
*
* @param view
*/
public void defaultScan(View view) {
//调用的是zxing自带的扫描页面
Intent intent = new Intent(getApplication(), CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
/**
* 自己定制扫描界面
* @param view
*/
public void dingZhiScan(View view) {
Intent intent = new Intent(getApplication(), CustomActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
/**
* 图片的解析
* @param view
*/
public void picScan(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
//处理扫描结果(在界面上显示)
if (null != data) {
//拿到传递回来的数据
Bundle bundle = data.getExtras();
if (bundle == null) {
return;
}
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
//解析得到的结果
String result = bundle.getString(CodeUtils.RESULT_STRING);
Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
}
}
}else if (requestCode == REQUEST_IMAGE){
//取回选中的那种图片uri路径
Uri uri = data.getData();
try {
//解析图片的方法...ImageUtil.getImageAbsolutePath(this, uri)通过uri路径得到图片在手机中的绝对路径
CodeUtils.analyzeBitmap(ImageUtil.getImageAbsolutePath(this, uri), new CodeUtils.AnalyzeCallback() {
//Bitmap mBitmap 解析的那张图片, String result解析的结果
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
Toast.makeText(MainActivity.this, "解析结果:" + result, Toast.LENGTH_LONG).show();
}
@Override
public void onAnalyzeFailed() {
Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
CustomActivity:
package com.dash.a06_qr_code;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.dash.zxinglibrary.activity.CaptureFragment;
import com.dash.zxinglibrary.activity.CodeUtils;
public class CustomActivity extends AppCompatActivity {
/**
* 二维码解析回调函数
*/
CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, String result) {//解析成功
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
bundle.putString(CodeUtils.RESULT_STRING, result);
resultIntent.putExtras(bundle);
CustomActivity.this.setResult(RESULT_OK, resultIntent);
CustomActivity.this.finish();
}
@Override
public void onAnalyzeFailed() {//解析失败
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
bundle.putString(CodeUtils.RESULT_STRING, "");
resultIntent.putExtras(bundle);
CustomActivity.this.setResult(RESULT_OK, resultIntent);
CustomActivity.this.finish();
}
};
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);
CaptureFragment captureFragment = new CaptureFragment();
//给扫描的fragment定制一个页面
CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
captureFragment.setAnalyzeCallback(analyzeCallback);
getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
flag = false;
//闪光灯
findViewById(R.id.linear1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!flag){
//打开
CodeUtils.isLightEnable(true);
flag = true;
}else {
CodeUtils.isLightEnable(false);
flag = false;
}
}
});
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
MyApplication:
package com.dash.a06_qr_code;
import android.app.Application;
import com.dash.zxinglibrary.activity.ZXingLibrary;
/**
* Created by Dash on 2017/12/4.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化zxing库
ZXingLibrary.initDisplayOpinion(this);
}
}
---------------------------------------------------------------------
color.xml:
<color name="scan_corner_color">#ff0000</color>
---------------------------------------------------------------------
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="默认页面扫描"
android:onClick="defaultScan"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="定制页面扫描"
android:onClick="dingZhiScan"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="选择图片解析"
android:onClick="picScan"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="生成"
android:onClick="shengCheng"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
-------------------------------------------------------------
acticity_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_my_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#AA333333"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingTop="15dp">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="闪光灯"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="封面"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="街景"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="翻译"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
-----------------------------------------------------------------------------
my_camera.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/preview_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.dash.zxinglibrary.view.ViewfinderView
android:id="@+id/viewfinder_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:inner_corner_color="@color/scan_corner_color"
app:inner_corner_length="30dp"
app:inner_corner_width="5dp"
app:inner_height="200dp"
app:inner_margintop="150dp"
app:inner_scan_bitmap="@drawable/scan_image"
app:inner_scan_iscircle="true"
app:inner_scan_speed="10"
app:inner_width="200dp" />
</FrameLayout>