Zingxing 默认的就是一个扫描的小框框,但是就这一个小框框远远满足不了现在的需求那就需要我们自己写一个扫描的界面了
先看图
接下来就要下实现这个界面的代码了
首先先来看看布局是怎么写的吧
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.erweima.CustomCapatrueActivity">
<FrameLayout
android:id="@+id/fl_zxing_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:id="@+id/flash_light"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#ffffff"
android:text="闪光灯"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:id="@+id/pic_scan"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#ffffff"
android:text="相册"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#ffffff"
android:text="扫题"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#ffffff"
android:text="翻译"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
接下来就是写代码了
public class CustomCapatrueActivity extends AppCompatActivity {
private LinearLayout flash_light;
private boolean flag = false;;
private LinearLayout pic_scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_capatrue);
flash_light = findViewById(R.id.flash_light);
pic_scan = findViewById(R.id.pic_scan);
CaptureFragment captureFragment = new CaptureFragment();
//设置自定义的...扫描布局
//给扫描的fragment定制一个页面
CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
//设置一个解析的监听回调
captureFragment.setAnalyzeCallback(analyzeCallback);
//使用扫描的fragment替换这个frameLayout
getSupportFragmentManager().beginTransaction().replace(R.id.fl_zxing_container, captureFragment).commit();
flash_light.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag) {
CodeUtils.isLightEnable(false);
flag = false;
}else {
CodeUtils.isLightEnable(true);
flag = true;
}
}
});
//扫描图片的点击事件
pic_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//隐式意图
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,1002);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1002){
//uri路径......需要把uri路径转换为绝对路径!!!!!!!!!!!!!!!!!!!file...new file(绝对路径)
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(CustomCapatrueActivity.this, "解析结果:" + result, Toast.LENGTH_LONG).show();
}
@Override
public void onAnalyzeFailed() {
Toast.makeText(CustomCapatrueActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 二维码解析回调函数
*/
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);
setResult(RESULT_OK, resultIntent);
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);
setResult(RESULT_OK, resultIntent);
finish();
}
};
}
对了还要再写一个布局 那就是扫描的那个小框框了
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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="#ff0000"
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>
这些要想实现的话就要添加一个依赖 Zxing
对了不需要初始化 如果不初始化的那就会报错的哟
对了还有权限
这些缺一不可
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />