lib -> zxing-lite
This commit is contained in:
@@ -50,5 +50,5 @@ dependencies {
|
||||
|
||||
implementation deps.easypermissions
|
||||
|
||||
implementation project(':lib')
|
||||
implementation project(':zxing-lite')
|
||||
}
|
||||
|
||||
@@ -18,21 +18,20 @@ package com.king.zxing.app;
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.provider.MediaStore;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.king.zxing.CameraScan;
|
||||
import com.king.zxing.CaptureActivity;
|
||||
import com.king.zxing.app.util.UriUtils;
|
||||
import com.king.zxing.util.CodeUtils;
|
||||
import com.king.zxing.util.LogUtils;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
@@ -50,9 +49,9 @@ import pub.devrel.easypermissions.EasyPermissions;
|
||||
*
|
||||
* 2、通过继承CaptureActivity或者CaptureFragment并自定义布局。(适用于大多场景,并无需关心扫码相关逻辑,自定义布局时需覆写getLayoutId方法)
|
||||
*
|
||||
* 3、在你项目的Activity或者Fragment中创建一个CaptureHelper并在相应的生命周期中调用CaptureHelper的周期。(适用于想在扫码界面写交互逻辑,又因为项目架构或其它原因,无法直接或间接继承CaptureActivity或CaptureFragment时使用)
|
||||
* 3、在你项目的Activity或者Fragment中实例化一个CameraScan即可。(适用于想在扫码界面写交互逻辑,又因为项目架构或其它原因,无法直接或间接继承CaptureActivity或CaptureFragment时使用)
|
||||
*
|
||||
* 4、参照CaptureHelper写一个自定义的扫码帮助类,其它步骤同方式3。(扩展高级用法,谨慎使用)
|
||||
* 4、继承CameraScan自己实现一个,可参照默认实现类DefaultCameraScan,其它步骤同方式3。(扩展高级用法,谨慎使用)
|
||||
*
|
||||
*/
|
||||
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{
|
||||
@@ -109,26 +108,28 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
||||
}
|
||||
|
||||
private void parsePhoto(Intent data){
|
||||
final String path = UriUtils.getImagePath(this,data);
|
||||
LogUtils.d("path:" + path);
|
||||
if(TextUtils.isEmpty(path)){
|
||||
return;
|
||||
}
|
||||
//异步解析
|
||||
asyncThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final String result = CodeUtils.parseCode(path);
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d("Jenly","result:" + result);
|
||||
Toast.makeText(getContext(),result,Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
// final String path = UriUtils.getImagePath(this,data);
|
||||
// LogUtils.d("path:" + path);
|
||||
// if(TextUtils.isEmpty(path)){
|
||||
// return;
|
||||
// }
|
||||
|
||||
try {
|
||||
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),data.getData());
|
||||
//异步解析
|
||||
asyncThread(() -> {
|
||||
final String result = CodeUtils.parseCode(bitmap);
|
||||
runOnUiThread(() -> {
|
||||
LogUtils.d("result:" + result);
|
||||
Toast.makeText(getContext(),result,Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import android.provider.DocumentsContract;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
|
||||
import com.king.zxing.util.LogUtils;
|
||||
|
||||
/**
|
||||
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
@@ -28,7 +30,7 @@ public final class UriUtils {
|
||||
//获取系統版本
|
||||
int currentapiVersion = Build.VERSION.SDK_INT;
|
||||
if(currentapiVersion> Build.VERSION_CODES.KITKAT){
|
||||
Log.d("uri=intent.getData :", "" + uri);
|
||||
LogUtils.d("uri=intent.getData :" + uri);
|
||||
if (DocumentsContract.isDocumentUri(context, uri)) {
|
||||
String docId = DocumentsContract.getDocumentId(uri);
|
||||
Log.d("getDocumentId(uri) :", "" + docId);
|
||||
|
||||
@@ -1 +1 @@
|
||||
include ':app', ':lib'
|
||||
include ':app', ':zxing-lite'
|
||||
|
||||
2
lib/.gitignore → zxing-lite/.gitignore
vendored
2
lib/.gitignore → zxing-lite/.gitignore
vendored
@@ -1 +1 @@
|
||||
/build
|
||||
/build
|
||||
@@ -1,12 +1,12 @@
|
||||
apply plugin: 'com.novoda.bintray-release'
|
||||
|
||||
//添加
|
||||
publish {
|
||||
userOrg = 'jenly'//bintray.com用户名
|
||||
groupId = 'com.king.zxing'//jcenter上的路径
|
||||
artifactId = 'zxing-lite'//项目名称
|
||||
publishVersion = app_version.versionName//版本号
|
||||
desc = 'ZXingLite for Android'//描述
|
||||
website = 'https://github.com/jenly1314/ZXingLite'//网站
|
||||
|
||||
apply plugin: 'com.novoda.bintray-release'
|
||||
|
||||
//添加
|
||||
publish {
|
||||
userOrg = 'jenly'//bintray.com用户名
|
||||
groupId = 'com.king.zxing'//jcenter上的路径
|
||||
artifactId = 'zxing-lite'//项目名称
|
||||
publishVersion = app_version.versionName//版本号
|
||||
desc = 'ZXingLite for Android'//描述
|
||||
website = 'https://github.com/jenly1314/ZXingLite'//网站
|
||||
|
||||
}
|
||||
@@ -1,51 +1,51 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply from: 'bintray.gradle'
|
||||
|
||||
android {
|
||||
compileSdkVersion build_versions.compileSdk
|
||||
buildToolsVersion build_versions.buildTools
|
||||
defaultConfig {
|
||||
minSdkVersion build_versions.minSdk
|
||||
targetSdkVersion build_versions.targetSdk
|
||||
versionCode app_version.versionCode
|
||||
versionName app_version.versionName
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
warning 'InvalidPackage'
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
//task javadoc(type: Javadoc) {
|
||||
// source = android.sourceSets.main.java.srcDirs
|
||||
// classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
|
||||
//}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
||||
testImplementation deps.test.junit
|
||||
androidTestImplementation deps.test.runner
|
||||
androidTestImplementation deps.test.espresso
|
||||
|
||||
compileOnly deps.androidx.appcompat
|
||||
api deps.zxing
|
||||
api deps.camera_core
|
||||
api deps.camera_camera2
|
||||
api deps.camera_lifecycle
|
||||
api deps.camera_view
|
||||
}
|
||||
apply plugin: 'com.android.library'
|
||||
apply from: 'bintray.gradle'
|
||||
|
||||
android {
|
||||
compileSdkVersion build_versions.compileSdk
|
||||
buildToolsVersion build_versions.buildTools
|
||||
defaultConfig {
|
||||
minSdkVersion build_versions.minSdk
|
||||
targetSdkVersion build_versions.targetSdk
|
||||
versionCode app_version.versionCode
|
||||
versionName app_version.versionName
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
warning 'InvalidPackage'
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
//task javadoc(type: Javadoc) {
|
||||
// source = android.sourceSets.main.java.srcDirs
|
||||
// classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
|
||||
//}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
||||
testImplementation deps.test.junit
|
||||
androidTestImplementation deps.test.runner
|
||||
androidTestImplementation deps.test.espresso
|
||||
|
||||
compileOnly deps.androidx.appcompat
|
||||
api deps.zxing
|
||||
api deps.camera_core
|
||||
api deps.camera_camera2
|
||||
api deps.camera_lifecycle
|
||||
api deps.camera_view
|
||||
}
|
||||
3
zxing-lite/gradle.properties
Normal file
3
zxing-lite/gradle.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
POM_NAME=ZXingLite
|
||||
POM_ARTIFACT_ID=zxing-lite
|
||||
POM_PACKAGING=aar
|
||||
@@ -1,34 +1,34 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
|
||||
#ZXingLite
|
||||
-dontwarn com.king.zxing.**
|
||||
-keep class com.king.zxing.**{ *;}
|
||||
-keepattributes InnerClasses
|
||||
-keepclassmembers enum * {
|
||||
public static **[] values();
|
||||
public static ** valueOf(java.lang.String);
|
||||
}
|
||||
|
||||
#ZXing
|
||||
-dontwarn com.google.zxing.**
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
|
||||
#ZXingLite
|
||||
-dontwarn com.king.zxing.**
|
||||
-keep class com.king.zxing.**{ *;}
|
||||
-keepattributes InnerClasses
|
||||
-keepclassmembers enum * {
|
||||
public static **[] values();
|
||||
public static ** valueOf(java.lang.String);
|
||||
}
|
||||
|
||||
#ZXing
|
||||
-dontwarn com.google.zxing.**
|
||||
-keep class com.google.zxing.**{ *;}
|
||||
@@ -1,27 +1,27 @@
|
||||
package com.king.zxing;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.king.zxing.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
package com.king.zxing;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.king.zxing.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.king.zxing">
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<uses-permission android:name="android.permission.FLASHLIGHT"/>
|
||||
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
|
||||
<application>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.king.zxing">
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<uses-permission android:name="android.permission.FLASHLIGHT"/>
|
||||
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
|
||||
<application>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -1,214 +1,214 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Jenly Yu
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.king.zxing;
|
||||
|
||||
import android.Manifest;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
import com.google.zxing.Result;
|
||||
import com.king.zxing.util.LogUtils;
|
||||
import com.king.zxing.util.PermissionUtils;
|
||||
|
||||
import androidx.annotation.LayoutRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.view.PreviewView;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public class CaptureActivity extends AppCompatActivity implements CameraScan.OnScanResultCallback{
|
||||
|
||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 0X86;
|
||||
|
||||
protected PreviewView previewView;
|
||||
protected ViewfinderView viewfinderView;
|
||||
protected View ivFlashlight;
|
||||
|
||||
private CameraScan mCameraScan;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
int layoutId = getLayoutId();
|
||||
if(isContentView(layoutId)){
|
||||
setContentView(layoutId);
|
||||
}
|
||||
initUI();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void initUI(){
|
||||
previewView = findViewById(getPreviewViewId());
|
||||
int viewfinderViewId = getViewfinderViewId();
|
||||
if(viewfinderViewId != 0){
|
||||
viewfinderView = findViewById(viewfinderViewId);
|
||||
}
|
||||
int ivFlashlightId = getFlashlightId();
|
||||
if(ivFlashlightId != 0){
|
||||
ivFlashlight = findViewById(ivFlashlightId);
|
||||
if(ivFlashlight != null){
|
||||
ivFlashlight.setOnClickListener(v -> onClickFlashlight());
|
||||
}
|
||||
}
|
||||
initCameraScan();
|
||||
startCamera();
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击手电筒
|
||||
*/
|
||||
protected void onClickFlashlight(){
|
||||
toggleTorchState();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化CameraScan
|
||||
*/
|
||||
public void initCameraScan(){
|
||||
mCameraScan = new DefaultCameraScan(this,previewView);
|
||||
mCameraScan.setOnScanResultCallback(this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启动相机预览
|
||||
*/
|
||||
public void startCamera(){
|
||||
if(mCameraScan != null){
|
||||
if(PermissionUtils.checkPermission(this,Manifest.permission.CAMERA)){
|
||||
mCameraScan.startCamera();
|
||||
}else{
|
||||
LogUtils.d("checkPermissionResult != PERMISSION_GRANTED");
|
||||
PermissionUtils.requestPermission(this,Manifest.permission.CAMERA,CAMERA_PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 释放相机
|
||||
*/
|
||||
private void releaseCamera(){
|
||||
if(mCameraScan != null){
|
||||
mCameraScan.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换闪光灯状态(开启/关闭)
|
||||
*/
|
||||
protected void toggleTorchState(){
|
||||
if(mCameraScan != null){
|
||||
boolean isTorch = mCameraScan.isTorchEnabled();
|
||||
mCameraScan.enableTorch(!isTorch);
|
||||
if(ivFlashlight != null){
|
||||
ivFlashlight.setSelected(!isTorch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if(requestCode == CAMERA_PERMISSION_REQUEST_CODE){
|
||||
requestCameraPermissionResult(permissions,grantResults);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求Camera权限回调结果
|
||||
* @param permissions
|
||||
* @param grantResults
|
||||
*/
|
||||
public void requestCameraPermissionResult(@NonNull String[] permissions, @NonNull int[] grantResults){
|
||||
if(PermissionUtils.requestPermissionsResult(Manifest.permission.CAMERA,permissions,grantResults)){
|
||||
startCamera();
|
||||
}else{
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
releaseCamera();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回true时会自动初始化{@link #setContentView(int)},返回为false是需自己去初始化{@link #setContentView(int)}
|
||||
* @param layoutId
|
||||
* @return 默认返回true
|
||||
*/
|
||||
public boolean isContentView(@LayoutRes int layoutId){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局id
|
||||
* @return
|
||||
*/
|
||||
public int getLayoutId(){
|
||||
return R.layout.zxl_capture;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #viewfinderView} 的 ID
|
||||
* @return 默认返回{@code R.id.viewfinderView}, 如果不需要扫码框可以返回0
|
||||
*/
|
||||
public int getViewfinderViewId(){
|
||||
return R.id.viewfinderView;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 预览界面{@link #previewView} 的ID
|
||||
* @return
|
||||
*/
|
||||
public int getPreviewViewId(){
|
||||
return R.id.previewView;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 {@link #ivFlashlight} 的ID
|
||||
* @return 默认返回{@code R.id.ivFlashlight}, 如果不需要手电筒按钮可以返回0
|
||||
*/
|
||||
public int getFlashlightId(){
|
||||
return R.id.ivFlashlight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get {@link CameraScan}
|
||||
* @return {@link #mCameraScan}
|
||||
*/
|
||||
public CameraScan getCameraScan(){
|
||||
return mCameraScan;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收扫码结果回调
|
||||
* @param result 扫码结果
|
||||
* @return 返回true表示拦截,将不自动执行后续逻辑,为false表示不拦截,默认不拦截
|
||||
*/
|
||||
@Override
|
||||
public boolean onScanResultCallback(Result result) {
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2018 Jenly Yu
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.king.zxing;
|
||||
|
||||
import android.Manifest;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
import com.google.zxing.Result;
|
||||
import com.king.zxing.util.LogUtils;
|
||||
import com.king.zxing.util.PermissionUtils;
|
||||
|
||||
import androidx.annotation.LayoutRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.view.PreviewView;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public class CaptureActivity extends AppCompatActivity implements CameraScan.OnScanResultCallback{
|
||||
|
||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 0X86;
|
||||
|
||||
protected PreviewView previewView;
|
||||
protected ViewfinderView viewfinderView;
|
||||
protected View ivFlashlight;
|
||||
|
||||
private CameraScan mCameraScan;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
int layoutId = getLayoutId();
|
||||
if(isContentView(layoutId)){
|
||||
setContentView(layoutId);
|
||||
}
|
||||
initUI();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void initUI(){
|
||||
previewView = findViewById(getPreviewViewId());
|
||||
int viewfinderViewId = getViewfinderViewId();
|
||||
if(viewfinderViewId != 0){
|
||||
viewfinderView = findViewById(viewfinderViewId);
|
||||
}
|
||||
int ivFlashlightId = getFlashlightId();
|
||||
if(ivFlashlightId != 0){
|
||||
ivFlashlight = findViewById(ivFlashlightId);
|
||||
if(ivFlashlight != null){
|
||||
ivFlashlight.setOnClickListener(v -> onClickFlashlight());
|
||||
}
|
||||
}
|
||||
initCameraScan();
|
||||
startCamera();
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击手电筒
|
||||
*/
|
||||
protected void onClickFlashlight(){
|
||||
toggleTorchState();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化CameraScan
|
||||
*/
|
||||
public void initCameraScan(){
|
||||
mCameraScan = new DefaultCameraScan(this,previewView);
|
||||
mCameraScan.setOnScanResultCallback(this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启动相机预览
|
||||
*/
|
||||
public void startCamera(){
|
||||
if(mCameraScan != null){
|
||||
if(PermissionUtils.checkPermission(this,Manifest.permission.CAMERA)){
|
||||
mCameraScan.startCamera();
|
||||
}else{
|
||||
LogUtils.d("checkPermissionResult != PERMISSION_GRANTED");
|
||||
PermissionUtils.requestPermission(this,Manifest.permission.CAMERA,CAMERA_PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 释放相机
|
||||
*/
|
||||
private void releaseCamera(){
|
||||
if(mCameraScan != null){
|
||||
mCameraScan.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换闪光灯状态(开启/关闭)
|
||||
*/
|
||||
protected void toggleTorchState(){
|
||||
if(mCameraScan != null){
|
||||
boolean isTorch = mCameraScan.isTorchEnabled();
|
||||
mCameraScan.enableTorch(!isTorch);
|
||||
if(ivFlashlight != null){
|
||||
ivFlashlight.setSelected(!isTorch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if(requestCode == CAMERA_PERMISSION_REQUEST_CODE){
|
||||
requestCameraPermissionResult(permissions,grantResults);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求Camera权限回调结果
|
||||
* @param permissions
|
||||
* @param grantResults
|
||||
*/
|
||||
public void requestCameraPermissionResult(@NonNull String[] permissions, @NonNull int[] grantResults){
|
||||
if(PermissionUtils.requestPermissionsResult(Manifest.permission.CAMERA,permissions,grantResults)){
|
||||
startCamera();
|
||||
}else{
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
releaseCamera();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回true时会自动初始化{@link #setContentView(int)},返回为false是需自己去初始化{@link #setContentView(int)}
|
||||
* @param layoutId
|
||||
* @return 默认返回true
|
||||
*/
|
||||
public boolean isContentView(@LayoutRes int layoutId){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局id
|
||||
* @return
|
||||
*/
|
||||
public int getLayoutId(){
|
||||
return R.layout.zxl_capture;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #viewfinderView} 的 ID
|
||||
* @return 默认返回{@code R.id.viewfinderView}, 如果不需要扫码框可以返回0
|
||||
*/
|
||||
public int getViewfinderViewId(){
|
||||
return R.id.viewfinderView;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 预览界面{@link #previewView} 的ID
|
||||
* @return
|
||||
*/
|
||||
public int getPreviewViewId(){
|
||||
return R.id.previewView;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 {@link #ivFlashlight} 的ID
|
||||
* @return 默认返回{@code R.id.ivFlashlight}, 如果不需要手电筒按钮可以返回0
|
||||
*/
|
||||
public int getFlashlightId(){
|
||||
return R.id.ivFlashlight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get {@link CameraScan}
|
||||
* @return {@link #mCameraScan}
|
||||
*/
|
||||
public CameraScan getCameraScan(){
|
||||
return mCameraScan;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收扫码结果回调
|
||||
* @param result 扫码结果
|
||||
* @return 返回true表示拦截,将不自动执行后续逻辑,为false表示不拦截,默认不拦截
|
||||
*/
|
||||
@Override
|
||||
public boolean onScanResultCallback(Result result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ import com.google.zxing.ResultPoint;
|
||||
import com.google.zxing.common.detector.MathUtils;
|
||||
import com.king.zxing.analyze.Analyzer;
|
||||
import com.king.zxing.analyze.MultiFormatAnalyzer;
|
||||
import com.king.zxing.manager.AmbientLightManager;
|
||||
import com.king.zxing.manager.BeepManager;
|
||||
import com.king.zxing.util.LogUtils;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,151 +1,151 @@
|
||||
package com.king.zxing;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2012 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public class AmbientLightManager implements SensorEventListener {
|
||||
|
||||
private static final int INTERVAL_TIME = 200;
|
||||
|
||||
protected static final float DARK_LUX = 45.0f;
|
||||
protected static final float BRIGHT_LUX = 100.0f;
|
||||
|
||||
/**
|
||||
* 光线太暗时,默认:照度45 lux
|
||||
*/
|
||||
private float darkLightLux = DARK_LUX;
|
||||
/**
|
||||
* 光线足够亮时,默认:照度100 lux
|
||||
*/
|
||||
private float brightLightLux = BRIGHT_LUX;
|
||||
|
||||
private SensorManager sensorManager;
|
||||
private Sensor lightSensor;
|
||||
|
||||
private long lastTime;
|
||||
|
||||
private boolean isLightSensorEnabled;
|
||||
|
||||
private OnLightSensorEventListener mOnLightSensorEventListener;
|
||||
|
||||
AmbientLightManager(Context context) {
|
||||
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
|
||||
isLightSensorEnabled = true;
|
||||
}
|
||||
|
||||
public void register() {
|
||||
if (sensorManager != null && lightSensor != null) {
|
||||
sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
}
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
if (sensorManager != null && lightSensor != null) {
|
||||
sensorManager.unregisterListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent sensorEvent) {
|
||||
if(isLightSensorEnabled){
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if(currentTime - lastTime < INTERVAL_TIME){//降低频率
|
||||
return;
|
||||
}
|
||||
lastTime = currentTime;
|
||||
|
||||
if (mOnLightSensorEventListener != null) {
|
||||
float lightLux = sensorEvent.values[0];
|
||||
mOnLightSensorEventListener.onSensorChanged(lightLux);
|
||||
if (lightLux <= darkLightLux) {
|
||||
mOnLightSensorEventListener.onSensorChanged(true,darkLightLux);
|
||||
} else if (lightLux >= brightLightLux) {
|
||||
mOnLightSensorEventListener.onSensorChanged(false,darkLightLux);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置光线足够暗的阈值(单位:lux)
|
||||
* @param lightLux
|
||||
*/
|
||||
public void setDarkLightLux(float lightLux){
|
||||
this.darkLightLux = lightLux;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置光线足够明亮的阈值(单位:lux)
|
||||
* @param lightLux
|
||||
*/
|
||||
public void setBrightLightLux(float lightLux){
|
||||
this.brightLightLux = lightLux;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public boolean isLightSensorEnabled() {
|
||||
return isLightSensorEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用光线亮度传感器
|
||||
* @param lightSensorEnabled
|
||||
*/
|
||||
public void setLightSensorEnabled(boolean lightSensorEnabled) {
|
||||
isLightSensorEnabled = lightSensorEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置光线亮度传感器监听器,只有在 {@link #isLightSensorEnabled} 为{@code true} 才有效
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnLightSensorEventListener(OnLightSensorEventListener listener){
|
||||
mOnLightSensorEventListener = listener;
|
||||
}
|
||||
|
||||
public interface OnLightSensorEventListener{
|
||||
/**
|
||||
*
|
||||
* @param lightLux 当前检测到的光线照度值
|
||||
*/
|
||||
default void onSensorChanged(float lightLux){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 传感器改变事件
|
||||
* @param dark 是否太暗了,当检测到的光线照度值小于{@link #darkLightLux}时,为{@code true}
|
||||
* @param lightLux 当前检测到的光线照度值
|
||||
*/
|
||||
void onSensorChanged(boolean dark,float lightLux);
|
||||
}
|
||||
}
|
||||
package com.king.zxing.manager;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2012 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public class AmbientLightManager implements SensorEventListener {
|
||||
|
||||
private static final int INTERVAL_TIME = 200;
|
||||
|
||||
protected static final float DARK_LUX = 45.0f;
|
||||
protected static final float BRIGHT_LUX = 100.0f;
|
||||
|
||||
/**
|
||||
* 光线太暗时,默认:照度45 lux
|
||||
*/
|
||||
private float darkLightLux = DARK_LUX;
|
||||
/**
|
||||
* 光线足够亮时,默认:照度100 lux
|
||||
*/
|
||||
private float brightLightLux = BRIGHT_LUX;
|
||||
|
||||
private SensorManager sensorManager;
|
||||
private Sensor lightSensor;
|
||||
|
||||
private long lastTime;
|
||||
|
||||
private boolean isLightSensorEnabled;
|
||||
|
||||
private OnLightSensorEventListener mOnLightSensorEventListener;
|
||||
|
||||
public AmbientLightManager(Context context) {
|
||||
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
|
||||
isLightSensorEnabled = true;
|
||||
}
|
||||
|
||||
public void register() {
|
||||
if (sensorManager != null && lightSensor != null) {
|
||||
sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
}
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
if (sensorManager != null && lightSensor != null) {
|
||||
sensorManager.unregisterListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent sensorEvent) {
|
||||
if(isLightSensorEnabled){
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if(currentTime - lastTime < INTERVAL_TIME){//降低频率
|
||||
return;
|
||||
}
|
||||
lastTime = currentTime;
|
||||
|
||||
if (mOnLightSensorEventListener != null) {
|
||||
float lightLux = sensorEvent.values[0];
|
||||
mOnLightSensorEventListener.onSensorChanged(lightLux);
|
||||
if (lightLux <= darkLightLux) {
|
||||
mOnLightSensorEventListener.onSensorChanged(true,lightLux);
|
||||
} else if (lightLux >= brightLightLux) {
|
||||
mOnLightSensorEventListener.onSensorChanged(false,lightLux);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置光线足够暗的阈值(单位:lux)
|
||||
* @param lightLux
|
||||
*/
|
||||
public void setDarkLightLux(float lightLux){
|
||||
this.darkLightLux = lightLux;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置光线足够明亮的阈值(单位:lux)
|
||||
* @param lightLux
|
||||
*/
|
||||
public void setBrightLightLux(float lightLux){
|
||||
this.brightLightLux = lightLux;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public boolean isLightSensorEnabled() {
|
||||
return isLightSensorEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用光线亮度传感器
|
||||
* @param lightSensorEnabled
|
||||
*/
|
||||
public void setLightSensorEnabled(boolean lightSensorEnabled) {
|
||||
isLightSensorEnabled = lightSensorEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置光线亮度传感器监听器,只有在 {@link #isLightSensorEnabled} 为{@code true} 才有效
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnLightSensorEventListener(OnLightSensorEventListener listener){
|
||||
mOnLightSensorEventListener = listener;
|
||||
}
|
||||
|
||||
public interface OnLightSensorEventListener{
|
||||
/**
|
||||
*
|
||||
* @param lightLux 当前检测到的光线照度值
|
||||
*/
|
||||
default void onSensorChanged(float lightLux){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 传感器改变事件
|
||||
* @param dark 是否太暗了,当检测到的光线照度值小于{@link #darkLightLux}时,为{@code true}
|
||||
* @param lightLux 当前检测到的光线照度值
|
||||
*/
|
||||
void onSensorChanged(boolean dark,float lightLux);
|
||||
}
|
||||
}
|
||||
@@ -1,110 +1,111 @@
|
||||
package com.king.zxing;
|
||||
/*
|
||||
* Copyright (C) 2010 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Vibrator;
|
||||
|
||||
import com.king.zxing.util.LogUtils;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public final class BeepManager implements MediaPlayer.OnErrorListener, Closeable {
|
||||
|
||||
private static final long VIBRATE_DURATION = 200L;
|
||||
|
||||
private final Context context;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private Vibrator vibrator;
|
||||
private boolean playBeep;
|
||||
private boolean vibrate;
|
||||
|
||||
BeepManager(Context context) {
|
||||
this.context = context;
|
||||
this.mediaPlayer = null;
|
||||
updatePrefs();
|
||||
}
|
||||
|
||||
public void setVibrate(boolean vibrate){
|
||||
this.vibrate = vibrate;
|
||||
}
|
||||
|
||||
public void setPlayBeep(boolean playBeep){
|
||||
this.playBeep = playBeep;
|
||||
}
|
||||
|
||||
private synchronized void updatePrefs() {
|
||||
if (mediaPlayer == null) {
|
||||
mediaPlayer = buildMediaPlayer(context);
|
||||
}
|
||||
if(vibrator == null){
|
||||
vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void playBeepSoundAndVibrate() {
|
||||
if (playBeep && mediaPlayer != null) {
|
||||
mediaPlayer.start();
|
||||
}
|
||||
if (vibrate) {
|
||||
vibrator.vibrate(VIBRATE_DURATION);
|
||||
}
|
||||
}
|
||||
|
||||
private MediaPlayer buildMediaPlayer(Context context) {
|
||||
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||
try {
|
||||
AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.zxl_beep);
|
||||
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
|
||||
mediaPlayer.setOnErrorListener(this);
|
||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
mediaPlayer.setLooping(false);
|
||||
mediaPlayer.prepare();
|
||||
return mediaPlayer;
|
||||
} catch (Exception e) {
|
||||
LogUtils.w(e);
|
||||
mediaPlayer.release();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
|
||||
close();
|
||||
updatePrefs();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
try{
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.release();
|
||||
mediaPlayer = null;
|
||||
}
|
||||
}catch (Exception e){
|
||||
LogUtils.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
package com.king.zxing.manager;
|
||||
/*
|
||||
* Copyright (C) 2010 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Vibrator;
|
||||
|
||||
import com.king.zxing.R;
|
||||
import com.king.zxing.util.LogUtils;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public final class BeepManager implements MediaPlayer.OnErrorListener, Closeable {
|
||||
|
||||
private static final long VIBRATE_DURATION = 200L;
|
||||
|
||||
private final Context context;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private Vibrator vibrator;
|
||||
private boolean playBeep;
|
||||
private boolean vibrate;
|
||||
|
||||
public BeepManager(Context context) {
|
||||
this.context = context;
|
||||
this.mediaPlayer = null;
|
||||
updatePrefs();
|
||||
}
|
||||
|
||||
public void setVibrate(boolean vibrate){
|
||||
this.vibrate = vibrate;
|
||||
}
|
||||
|
||||
public void setPlayBeep(boolean playBeep){
|
||||
this.playBeep = playBeep;
|
||||
}
|
||||
|
||||
private synchronized void updatePrefs() {
|
||||
if (mediaPlayer == null) {
|
||||
mediaPlayer = buildMediaPlayer(context);
|
||||
}
|
||||
if(vibrator == null){
|
||||
vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void playBeepSoundAndVibrate() {
|
||||
if (playBeep && mediaPlayer != null) {
|
||||
mediaPlayer.start();
|
||||
}
|
||||
if (vibrate) {
|
||||
vibrator.vibrate(VIBRATE_DURATION);
|
||||
}
|
||||
}
|
||||
|
||||
private MediaPlayer buildMediaPlayer(Context context) {
|
||||
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||
try {
|
||||
AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.zxl_beep);
|
||||
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
|
||||
mediaPlayer.setOnErrorListener(this);
|
||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
mediaPlayer.setLooping(false);
|
||||
mediaPlayer.prepare();
|
||||
return mediaPlayer;
|
||||
} catch (Exception e) {
|
||||
LogUtils.w(e);
|
||||
mediaPlayer.release();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
|
||||
close();
|
||||
updatePrefs();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
try{
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.release();
|
||||
mediaPlayer = null;
|
||||
}
|
||||
}catch (Exception e){
|
||||
LogUtils.e(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
@@ -1,46 +1,46 @@
|
||||
<resources>
|
||||
<declare-styleable name="ViewfinderView">
|
||||
<attr name="maskColor" format="color" />
|
||||
<attr name="frameColor" format="color" />
|
||||
<attr name="cornerColor" format="color"/>
|
||||
<attr name="laserColor" format="color"/>
|
||||
<attr name="labelText" format="string"/>
|
||||
<attr name="labelTextColor" format="color"/>
|
||||
<attr name="labelTextSize" format="dimension"/>
|
||||
<attr name="labelTextPadding" format="dimension"/>
|
||||
<attr name="labelTextWidth" format="dimension"/>
|
||||
<attr name="labelTextLocation" format="enum">
|
||||
<enum name="top" value="0"/>
|
||||
<enum name="bottom" value="1"/>
|
||||
</attr>
|
||||
<attr name="frameWidth" format="dimension"/>
|
||||
<attr name="frameHeight" format="dimension"/>
|
||||
<attr name="gridColumn" format="integer"/>
|
||||
<attr name="gridHeight" format="dimension"/>
|
||||
<attr name="laserStyle" format="enum">
|
||||
<enum name="none" value="0"/>
|
||||
<enum name="line" value="1"/>
|
||||
<enum name="grid" value="2"/>
|
||||
</attr>
|
||||
<attr name="cornerRectWidth" format="dimension"/>
|
||||
<attr name="cornerRectHeight" format="dimension"/>
|
||||
<attr name="scannerLineMoveDistance" format="dimension"/>
|
||||
<attr name="scannerLineHeight" format="dimension"/>
|
||||
<attr name="frameLineWidth" format="dimension"/>
|
||||
<attr name="scannerAnimationDelay" format="integer"/>
|
||||
<attr name="frameRatio" format="float"/>
|
||||
<attr name="framePaddingLeft" format="dimension"/>
|
||||
<attr name="framePaddingTop" format="dimension"/>
|
||||
<attr name="framePaddingRight" format="dimension"/>
|
||||
<attr name="framePaddingBottom" format="dimension"/>
|
||||
<attr name="frameGravity" format="enum">
|
||||
<enum name="center" value="0"/>
|
||||
<enum name="left" value="1"/>
|
||||
<enum name="top" value="2"/>
|
||||
<enum name="right" value="3"/>
|
||||
<enum name="bottom" value="4"/>
|
||||
</attr>
|
||||
</declare-styleable>
|
||||
|
||||
|
||||
<resources>
|
||||
<declare-styleable name="ViewfinderView">
|
||||
<attr name="maskColor" format="color" />
|
||||
<attr name="frameColor" format="color" />
|
||||
<attr name="cornerColor" format="color"/>
|
||||
<attr name="laserColor" format="color"/>
|
||||
<attr name="labelText" format="string"/>
|
||||
<attr name="labelTextColor" format="color"/>
|
||||
<attr name="labelTextSize" format="dimension"/>
|
||||
<attr name="labelTextPadding" format="dimension"/>
|
||||
<attr name="labelTextWidth" format="dimension"/>
|
||||
<attr name="labelTextLocation" format="enum">
|
||||
<enum name="top" value="0"/>
|
||||
<enum name="bottom" value="1"/>
|
||||
</attr>
|
||||
<attr name="frameWidth" format="dimension"/>
|
||||
<attr name="frameHeight" format="dimension"/>
|
||||
<attr name="gridColumn" format="integer"/>
|
||||
<attr name="gridHeight" format="dimension"/>
|
||||
<attr name="laserStyle" format="enum">
|
||||
<enum name="none" value="0"/>
|
||||
<enum name="line" value="1"/>
|
||||
<enum name="grid" value="2"/>
|
||||
</attr>
|
||||
<attr name="cornerRectWidth" format="dimension"/>
|
||||
<attr name="cornerRectHeight" format="dimension"/>
|
||||
<attr name="scannerLineMoveDistance" format="dimension"/>
|
||||
<attr name="scannerLineHeight" format="dimension"/>
|
||||
<attr name="frameLineWidth" format="dimension"/>
|
||||
<attr name="scannerAnimationDelay" format="integer"/>
|
||||
<attr name="frameRatio" format="float"/>
|
||||
<attr name="framePaddingLeft" format="dimension"/>
|
||||
<attr name="framePaddingTop" format="dimension"/>
|
||||
<attr name="framePaddingRight" format="dimension"/>
|
||||
<attr name="framePaddingBottom" format="dimension"/>
|
||||
<attr name="frameGravity" format="enum">
|
||||
<enum name="center" value="0"/>
|
||||
<enum name="left" value="1"/>
|
||||
<enum name="top" value="2"/>
|
||||
<enum name="right" value="3"/>
|
||||
<enum name="bottom" value="4"/>
|
||||
</attr>
|
||||
</declare-styleable>
|
||||
|
||||
|
||||
</resources>
|
||||
@@ -1,13 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<color name="viewfinder_mask">#60000000</color>
|
||||
<color name="viewfinder_frame">#7F1FB3E2</color>
|
||||
<color name="viewfinder_corner">#FF1FB3E2</color>
|
||||
<color name="viewfinder_laser">#FF1FB3E2</color>
|
||||
<color name="viewfinder_text_color">#FFC0C0C0</color>
|
||||
|
||||
<color name="zxl_capture_status_bar_color">#00000000</color>
|
||||
<color name="zxl_capture_navigation_bar_color">#00000000</color>
|
||||
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<color name="viewfinder_mask">#60000000</color>
|
||||
<color name="viewfinder_frame">#7F1FB3E2</color>
|
||||
<color name="viewfinder_corner">#FF1FB3E2</color>
|
||||
<color name="viewfinder_laser">#FF1FB3E2</color>
|
||||
<color name="viewfinder_text_color">#FFC0C0C0</color>
|
||||
|
||||
<color name="zxl_capture_status_bar_color">#00000000</color>
|
||||
<color name="zxl_capture_navigation_bar_color">#00000000</color>
|
||||
|
||||
</resources>
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.king.zxing;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
package com.king.zxing;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user