总体的步骤:
1: 首先导入相关的项目
2: demo是两个activity,从MainActivity跳转到ScanActivity,然后将从ScanActivity里面获取的传给MainActivity
3: 考虑可能需要打开闪光灯,添加一个切换闪光灯的,按钮,并且添加一个闪光灯的判断boolean
1: 需要导入的zxing的相关的
compile 'com.journeyapps:zxing-android-embedded:3.3.0'
compile 'com.google.zxing:core:3.2.1'
2: MainActivity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 点击跳转
* 设置方向不锁住
* 跳转的activity
* @param view
*/
public void jumpToScan(View view) {
new IntentIntegrator(this)
.setOrientationLocked(false)
.setCaptureActivity(ScanActivity.class)
.initiateScan();
}
/**
* 从返回的信息里面找到扫描到的条码
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//通过IntentIntegrator.parseActivityResult 来获取结果
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(intentResult!=null){
//获取扫描到的数据
String contents = intentResult.getContents();
Toast.makeText(this,"扫描到:"+contents,Toast.LENGTH_LONG).show();
}
}
}
activate_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kodulf.myzxingdemo.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="jumpToScan"
android:onClick="jumpToScan"
app:layout_constraintBottom_toBottomOf="@+id/activity_main"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintRight_toRightOf="@+id/activity_main"
app:layout_constraintTop_toTopOf="@+id/activity_main" />
</android.support.constraint.ConstraintLayout>
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
public class ScanActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {
/**
* 截图管理
*/
private CaptureManager captureManager;
/**
* 扫描的view
*/
private DecoratedBarcodeView barcodeView;
/**
* 当前闪光灯是否打开了
*/
private boolean isTorchOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
barcodeView = (DecoratedBarcodeView)findViewById(R.id.barcodeView);
barcodeView.setTorchListener(this);
//初始化,并且开始decorde
captureManager = new CaptureManager(this, barcodeView);
captureManager.initializeFromIntent(getIntent(),savedInstanceState);
captureManager.decode();
}
/**
* 这里需要将captureManager也考虑进去
*/
@Override
protected void onPause() {
super.onPause();
if(captureManager!=null){
captureManager.onPause();
}
}
/**
* 这里需要将captureManager也考虑进去
*/
@Override
protected void onDestroy() {
super.onDestroy();
if(captureManager!=null){
captureManager.onDestroy();
}
}
/**
* 这里需要将captureManager也考虑进去
*/
@Override
protected void onResume() {
super.onResume();
if(captureManager!=null){
captureManager.onResume();
}
}
/**
* 这里需要将captureManager也考虑进去
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(captureManager!=null){
captureManager.onSaveInstanceState(savedInstanceState);
}
}
@Override
public void onTorchOn() {
isTorchOn = true;
}
@Override
public void onTorchOff() {
isTorchOn = false;
}
/**
* 切换闪光灯
* @param view
*/
public void openOrCloseTorch(View view) {
if(isTorchOn){
if(hasTorch())
barcodeView.setTorchOff();
}else{
if(hasTorch())
barcodeView.setTorchOn();
}
}
/**
* 判断是否有闪光灯
* @return
*/
public boolean hasTorch(){
return getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
}
activity_scan.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kodulf.ScanActivity">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/barcodeView"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:zxing_framing_rect_width="200dp"
app:zxing_framing_rect_height="200dp"
app:zxing_preview_scaling_strategy="fitXY"
app:zxing_use_texture_view="true"
android:layout_marginBottom="60dp"
/>
<Button
android:layout_alignParentBottom="true"
android:id="@+id/swithTorch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="切换闪光灯"
android:onClick="openOrCloseTorch"/>
</RelativeLayout>
参考:http://blog.csdn.net/qq_28057541/article/details/52034988