Skip to content

Commit

Permalink
1.修改二维码扫描默认属性样式
Browse files Browse the repository at this point in the history
2.XQRCode新增API
3.修改使用说明
4.发布1.0.4版本
  • Loading branch information
xuexiangjys committed May 18, 2019
1 parent 01d7f80 commit da84a59
Show file tree
Hide file tree
Showing 30 changed files with 212 additions and 92 deletions.
106 changes: 88 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,58 @@ allprojects {
```
dependencies {
...
implementation 'com.github.xuexiangjys:XQRCode:1.0.3'
implementation 'com.github.xuexiangjys:XQRCode:1.0.4'
}
```

### 2.2、二维码扫描

#### 默认二维码扫描

1.默认二维码扫描界面`CaptureActivity`

二维码的扫描结果通过Intent返回出来:
使用`XQRCode.startScan`直接调取默认二维码扫描。

```
XQRCode.startScan(this, REQUEST_CODE);
```

2.二维码的扫描结果通过Intent返回出来:

* `XQRCode.RESULT_TYPE`:扫描结果类型,`XQRCode.RESULT_SUCCESS`代表扫描成功,`XQRCode.RESULT_FAILED`代表扫描失败。
* `XQRCode.RESULT_DATA`:扫描二维码的数据内容。

3.自定义默认二维码扫描界面的主题样式:

```
<!-- 自定义默认二维码扫描界面的主题. -->
<style name="XQRCodeTheme.Custom">
<item name="ViewfinderViewStyle">@style/ViewfinderView.Custom</item>
</style>
<style name="ViewfinderView.Custom">
<item name="inner_corner_color">#123456</item>
<item name="inner_corner_length">50dp</item>
<item name="inner_corner_width">5dp</item>
<item name="inner_scan_speed">20dp</item>
<item name="inner_scan_isCircle">false</item>
</style>
```

下面的二维码扫描代码仅供参考:

```
/**
* 开启二维码扫描
*/
@Permission(CAMERA)
@IOThread(ThreadType.Single)
private void startScan(ScanType scanType) {
switch(scanType) {
case CUSTOM:
openPageForResult(CustomCaptureFragment.class, null, REQUEST_CUSTOM_SCAN);
break;
switch (scanType) {
case DEFAULT:
startActivityForResult(new Intent(getActivity(), CaptureActivity.class), REQUEST_CODE);
XQRCode.startScan(this, REQUEST_CODE);
break;
case DEFAULT_Custom:
XQRCode.startScan(this, REQUEST_CODE, R.style.XQRCodeTheme_Custom);
break;
case REMOTE:
Intent intent = new Intent(XQRCode.ACTION_DEFAULT_CAPTURE);
Expand All @@ -82,6 +108,24 @@ private void startScan(ScanType scanType) {
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//处理二维码扫描结果
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//处理扫描结果(在界面上显示)
handleScanResult(data);
}
//选择系统图片并解析
else if (requestCode == REQUEST_IMAGE) {
if (data != null) {
Uri uri = data.getData();
getAnalyzeQRCodeResult(uri);
}
}
}
/**
* 处理二维码扫描结果
* @param data
Expand All @@ -99,11 +143,30 @@ private void handleScanResult(Intent data) {
}
}
}
/**
* 进行二维码解析
*
* @param uri
*/
private void getAnalyzeQRCodeResult(Uri uri) {
XQRCode.analyzeQRCode(PathUtils.getFilePathByUri(getContext(), uri), new QRCodeAnalyzeUtils.AnalyzeCallback() {
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
ToastUtils.toast("解析结果:" + result, Toast.LENGTH_LONG);
}
@Override
public void onAnalyzeFailed() {
ToastUtils.toast("解析二维码失败", Toast.LENGTH_LONG);
}
});
}
```

2.自定义二维码扫描界面
#### 自定义二维码扫描

(1)自定义一个扫码界面布局。自定义的扫码界面需要定义一个`SurfaceView`和一个`ViewfinderView`,且id必须是`preview_view``viewfinder_view`。详情见如下布局代码:
1. 自定义一个扫码界面布局。自定义的扫码界面需要定义一个`SurfaceView`和一个`ViewfinderView`,且id必须是`preview_view``viewfinder_view`。详情见如下布局代码:

```
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand All @@ -126,7 +189,7 @@ private void handleScanResult(Intent data) {
app:inner_marginTop="120dp"
app:inner_scan_bitmap="@mipmap/ic_scan_image"
app:inner_scan_isCircle="false"
app:inner_scan_speed="10"
app:inner_scan_speed="10dp"
app:inner_height="300dp"
app:inner_width="300dp" />
Expand All @@ -140,15 +203,15 @@ ViewfinderView属性表
inner_width | dimension | 屏幕宽度的3/4 | 扫描框的宽度
inner_height | dimension | 屏幕宽度的3/4 | 扫描框的高度
inner_marginTop | dimension | 居中效果 | 扫描框距离顶部的距离
inner_corner_color | color | #45DDDD | 扫描框四角的颜色
inner_corner_length | dimension | 65px | 扫描框四角的长度
inner_corner_width | dimension | 15px | 扫描框四角的宽度
inner_corner_color | color | #0DC2FE | 扫描框四角的颜色
inner_corner_length | dimension | 32dp | 扫描框四角的长度
inner_corner_width | dimension | 6dp | 扫描框四角的宽度
inner_scan_bitmap | reference | R.drawable.xqrcode_ic_scan_light | 扫描控件图资源
inner_scan_speed | integer | 5px | 扫描速度
inner_scan_speed | dimension | 5dp | 扫描速度
inner_scan_isCircle | boolean | true | 小圆点是否展示


(2)调用`XQRCode.getCaptureFragment`的方法,传入自定义扫描界面的布局ID,可以获得带扫描功能的Fragment-`CaptureFragment`,将其填充到页面中。
2. 调用`XQRCode.getCaptureFragment`的方法,传入自定义扫描界面的布局ID,可以获得带扫描功能的Fragment-`CaptureFragment`,将其填充到页面中。

```
// 为二维码扫描界面设置定制化界面
Expand All @@ -157,7 +220,14 @@ captureFragment.setAnalyzeCallback(analyzeCallback);
getChildFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
```

(3)最后为CaptureFragment设置二维码解析回调接口`AnalyzeCallback`即可。
3. 最后为CaptureFragment设置二维码解析回调接口`AnalyzeCallback`即可。

#### 设置相机聚焦的间隔

```
//设置相机的自动聚焦间隔
XQRCode.setAutoFocusInterval(1500L);
```

### 2.3、二维码生成

Expand Down Expand Up @@ -205,7 +275,7 @@ https://github.com/yipianfengye/android-zxingLibrary

![](https://github.com/xuexiangjys/XPage/blob/master/img/qq_group.jpg)

[xqsvg]: https://img.shields.io/badge/XQRCode-v1.0.3-brightgreen.svg
[xqsvg]: https://img.shields.io/badge/XQRCode-v1.0.4-brightgreen.svg
[xq]: https://github.com/xuexiangjys/XQRCode
[apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
[api]: https://android-arsenal.com/api?level=14
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'com.xuexiang.xaop' //引用xaop插件
//apply plugin: 'img-optimizer'

android {
compileSdkVersion build_versions.target_sdk
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/xuexiang/xqrcodedemo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.xuexiang.xpage.PageConfig;
import com.xuexiang.xpage.PageConfiguration;
import com.xuexiang.xpage.model.PageInfo;
import com.xuexiang.xqrcode.XQRCode;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.tip.ToastUtils;
Expand Down Expand Up @@ -73,6 +74,8 @@ public void onDenied(List<String> permissionsDenied) {

@Permission(CAMERA)
private void initPermission() {
//设置相机的自动聚焦间隔
XQRCode.setAutoFocusInterval(1500L);
ToastUtils.toast("相机权限已获取!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import static com.xuexiang.xqrcode.XQRCode.KEY_SCAN_INTERVAL;

/**
* 二维码扫描
* 二维码扫描
*
* @author xuexiang
* @since 2019/1/16 下午11:56
Expand Down Expand Up @@ -78,6 +78,7 @@ public class MainFragment extends XPageSimpleListFragment {
@Override
protected List<String> initSimpleData(List<String> lists) {
lists.add("默认扫描界面");
lists.add("默认扫描界面(自定义主题)");
lists.add("定制化扫描界面(单次)");
lists.add("定制化扫描界面(多次)");
lists.add("远程扫描界面");
Expand All @@ -98,18 +99,21 @@ protected void onItemClick(int position) {
startScan(ScanType.DEFAULT);
break;
case 1:
startScan(ScanType.CUSTOM_SINGLE);
startScan(ScanType.DEFAULT_Custom);
break;
case 2:
startScan(ScanType.CUSTOM_MULTIPLE);
startScan(ScanType.CUSTOM_SINGLE);
break;
case 3:
startScan(ScanType.REMOTE);
startScan(ScanType.CUSTOM_MULTIPLE);
break;
case 4:
openPage(QRCodeProduceFragment.class);
startScan(ScanType.REMOTE);
break;
case 5:
openPage(QRCodeProduceFragment.class);
break;
case 6:
selectQRCode();
break;
default:
Expand All @@ -126,18 +130,20 @@ private void selectQRCode() {
* 开启二维码扫描
*/
@Permission(CAMERA)
@IOThread(ThreadType.Single)
private void startScan(ScanType scanType) {
switch (scanType) {
case DEFAULT:
XQRCode.startScan(this, REQUEST_CODE);
break;
case DEFAULT_Custom:
XQRCode.startScan(this, REQUEST_CODE, R.style.XQRCodeTheme_Custom);
break;
case CUSTOM_SINGLE:
openPageForResult(CustomCaptureFragment.class, getScanParam(false, 0), REQUEST_CUSTOM_SCAN);
break;
case CUSTOM_MULTIPLE:
openPage(CustomCaptureFragment.class, getScanParam(true, 1000));
break;
case DEFAULT:
XQRCode.startScan(this, REQUEST_CODE, R.style.XQRCodeTheme_Custom);
break;
case REMOTE:
Intent intent = new Intent(XQRCode.ACTION_DEFAULT_CAPTURE);
startActivityForResult(intent, REQUEST_CODE);
Expand Down Expand Up @@ -187,6 +193,11 @@ else if (requestCode == REQUEST_IMAGE) {
}
}

/**
* 进行二维码解析
*
* @param uri
*/
@SuppressLint("MissingPermission")
private void getAnalyzeQRCodeResult(Uri uri) {
XQRCode.analyzeQRCode(PathUtils.getFilePathByUri(getContext(), uri), new QRCodeAnalyzeUtils.AnalyzeCallback() {
Expand Down Expand Up @@ -252,6 +263,10 @@ public enum ScanType {
* 默认
*/
DEFAULT,
/**
* 默认(修改主题)
*/
DEFAULT_Custom,
/**
* 远程
*/
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/layout_custom_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
app:inner_marginTop="80dp"
app:inner_scan_bitmap="@mipmap/ic_scan_image"
app:inner_scan_isCircle="false"
app:inner_scan_speed="10"
app:inner_scan_speed="10dp"
app:inner_height="300dp"
app:inner_width="300dp" />

Expand Down
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

<style name="ViewfinderView.Custom">
<item name="inner_corner_color">#123456</item>
<item name="inner_corner_length">50dp</item>
<item name="inner_corner_width">5dp</item>
<item name="inner_scan_speed">20dp</item>
<item name="inner_scan_isCircle">false</item>
</style>

</resources>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ buildscript {
classpath deps.android_maven_gradle_plugin

classpath 'com.github.xuexiangjys.XAOP:xaop-plugin:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.chenenyu:img-optimizer:1.1.1' // 图片压缩

}
}

Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ allprojects {
```
dependencies {
...
implementation 'com.github.xuexiangjys:XQRCode:1.0.3'
implementation 'com.github.xuexiangjys:XQRCode:1.0.4'
}
```

Expand All @@ -39,7 +39,7 @@ https://github.com/yipianfengye/android-zxingLibrary

[![](https://img.shields.io/badge/点击一键加入QQ交流群-602082750-blue.svg)](http://shang.qq.com/wpa/qunwpa?idkey=9922861ef85c19f1575aecea0e8680f60d9386080a97ed310c971ae074998887)

[xqsvg]: https://img.shields.io/badge/XQRCode-v1.0.3-brightgreen.svg
[xqsvg]: https://img.shields.io/badge/XQRCode-v1.0.4-brightgreen.svg
[xq]: https://github.com/xuexiangjys/XQRCode
[apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
[api]: https://android-arsenal.com/api?level=14
1 change: 1 addition & 0 deletions xqrcode-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
//apply plugin: 'img-optimizer'

android {
compileSdkVersion build_versions.target_sdk
Expand Down
Loading

0 comments on commit da84a59

Please sign in to comment.