http://blog.csdn.net/qq_28057541/article/details/52034988
http://blog.csdn.net/sankoshine/article/details/50823238?skin=ink
扫描框自定义仿微信样式http://download.csdn.net/detail/z_zt_t/9659802
gradle文件引用dependencies {
compile 'com.google.zxing:core:3.3.0'
compile 'com.journeyapps:zxing-android-embedded:3.3.0'
}
扫描返回结果
@Override
// 通过 onActivityResult的方法获取 扫描回来的 值 扫描二维码返回
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if(intentResult != null) {
if(intentResult.getContents() != null) {
// ScanResult 为 获取到的字符串
String ScanResult = intentResult.getContents();
HLog.v(TAG,"ScanResult",ScanResult);
//扫描二维码
if(!TextUtils.isEmpty(ScanResult)){
isScanHandler.removeMessages(0);
HkApplication.toWebView(ScanResult,MainActivity.this);
}else{
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
跳转到扫描二维码activity
// 你也可以使用简单的扫描功能,但是一般扫描的样式和行为都是可以自定义的,这里就写关于自定义的代码了
// 你可以把这个方法作为一个点击事件
public void customScan(){
new IntentIntegrator(this)
.setOrientationLocked(false)
.setCaptureActivity(ZxingScanActivity.class) // 设置自定义的activity是CustomActivity
.initiateScan(); // 初始化扫描
isScanHandler.sendEmptyMessageDelayed(0,30000);//30秒未扫描到,提示
}
扫描二维码activity
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
/**
* 扫描二维码
* Created by ypp on 2017/4/8.
*/
public class ZxingScanActivity extends Activity implements View.OnClickListener{
private final String TAG="ZxingScanActivity";
private CaptureManager captureManager;//扫描
private DecoratedBarcodeView dbv_custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_zxing_scan);
dbv_custom=(DecoratedBarcodeView)findViewById(R.id.dbv_custom);
//重要代码,初始化捕获
captureManager = new CaptureManager(this,dbv_custom);
captureManager.initializeFromIntent(getIntent(),savedInstanceState);
captureManager.decode();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
},29000);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
captureManager.onSaveInstanceState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return dbv_custom.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.left_btn:
finish();
break;
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/black">
<RelativeLayout
android:id="@+id/title_layout"
style="@style/title_bar_layout_style"
android:background="@drawable/title_bar_bg">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/scan_for_awhile"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/left_btn"
style="@style/title_back_btn_style"
android:layout_width="55dip"
android:layout_height="match_parent" />
</RelativeLayout>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/title_layout"/>
<!-- 这个控件就是扫描的窗口了 -->
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dbv_custom"
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_centerInParent="true"
android:layout_below="@id/title_layout"/>
</RelativeLayout>