03_Android集成zxing并自定义扫码界面 一.zxing_core jar包生成 下载zxing sdk 下载完成和可以看到zxing
下载zxing sdk
下载完成和可以看到zxing sdk的目录结构如下
从zxing sdk的android目录下拷贝CaptuReactivity.java文件到lib_zxing中,并做相应修改,同时根据CaptureActivity.java拷贝其他用到的java文件、xml文件
最后不要忘了,在android6.0及以后的版本需要收到去申请照相机、闪光灯等权限
按照上面的步骤一步步的进行修改,直至项目不报错位置,如果觉得麻烦,可以去下载我修改好的
三.竖屏不识别条形码问题解决 按照上面的步骤集成后会发现,在竖屏情况下不能识别条线码,因此需要对其进行修改,使其能在竖屏模式下识别条形码,参考将zxing扫码界面改为竖屏的问题进行修改限制CaptureActivity为竖屏显示,并禁用横屏切换
将:
if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATioN, true)) {
setRequestedOrientation(getCurrentOrientation());
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
改为:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRaiT);
在CameraManager中,修改预览缩放。主要根据长宽比对预览效果进行了分类。如果不需要横屏了,可以直接取竖屏的设置
将:
rect.left = rect.left * cameraResolution.x / screenResolution.x;
rect.right = rect.right * cameraResolution.x / screenResolution.x;
rect.top = rect.top * cameraResolution.y / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
改为:
if (screenResolution.x < screenResolution.y) {
// 下面为竖屏模式
rect.left = framingRect.left * cameraResolution.y / screenResolution.x;
rect.right = framingRect.right * cameraResolution.y / screenResolution.x;
rect.top = framingRect.top * cameraResolution.x / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.x / screenResolution.y;
} else {
// 下面为横屏模式
rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;
}
在CameraManager中,修改解析函数 buildLuminanceSource(),根据竖屏条件,将获得数据的宽和高置换,使其适应竖屏环境
将:
return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
改为:
PlanarYUVLuminanceSource source;
Point point = configManager.getScreenResolution();
if (point.x < point.y) {
byte[] rotatedData = new byte[data.length];
int newWidth = height;
int newHeight = width;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * newWidth + newWidth - 1 - y] = data[x + y * width];
}
source = new PlanarYUVLuminanceSource(rotatedData, newWidth, newHeight, rect.left, rect.top, rect.width(), rect.height(), false);
} else {
source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
}
return source;
在CameraConfigurationUtils中,需要在findBestPreviewSizeValue方法中将screenAspectRatio分情况设置,因为screenAspectRatio的要求总是大值比小值
将:
double screenAspectRatio = screenResolution.x / (double) screenResolution.y;
改为:
double screenAspectRatio = 0;
if(screenResolution.x < screenResolution.y) {
//竖屏
screenAspectRatio = screenResolution.y / (double) screenResolution.x;
} else {
//横屏
screenAspectRatio = screenResolution.x / (double) screenResolution.y;
}
在CameraConfigurationUtils中,删除如下代码,原因是对于镜头分辨率高,而屏幕分辨率低的手机,这段代码直接导致扫码插件采用较低的分辨率去生成用于解析的位图,所以直接去掉。然后你就会发现低分辨率的手机,对二维码、条码的识别率显著提高。
if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
Point exactPoint = new Point(realWidth, realHeight);
Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
return exactPoint;
}
四.扫码结果获取
在CaptureActivity的handleDecode方法中增加如下代码:
String scanResult = rawResult.getText();
Intent intent = new Intent();
intent.putExtra("scanResult", !TextUtils.isEmpty(scanResult) ? scanResult:"");
setResult(Activity.RESULT_OK, intent);
finish();
在跳转CaptureActivity时通过startActivityForResult进行跳转,在onActivityResult回调方法中即可获取扫描结果
Intent intent = new Intent(mContext, CaptureActivity.class);
startActivityForResult(intent, 101);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 101:
if (resultCode == RESULT_OK) {
String scanResult = data.getStringExtra("scanResult");
mTvResult.setText("扫描结果:" + scanResult);
}
break;
}
}
五.扫码界面自定义
从上面的图中可以看出,zxing的demo提供的扫码界面不能满足我们的要求,可以说是有点丑
--结束END--
本文标题: 03_android集成zxing并自定义扫码界面
本文链接: https://lsjlt.com/news/29613.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0