deniedPermissions) {
+ Toast.makeText(MainActivity.this, "您没有给权限,请检查", Toast.LENGTH_LONG).show();
+ }
+ }).start();
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+
+ // 扫描二维码/条码回传
+ if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
+ if (data != null) {
+ String content = data.getStringExtra(DECODED_CONTENT_KEY);
+ resultTv.setText("扫描结果为:" + content);
+ }
+ }
+ }
+}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
new file mode 100644
index 0000000..61fdba2
--- /dev/null
+++ b/app/src/main/res/layout/activity_main.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000..cde69bc
Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ
diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
new file mode 100644
index 0000000..9a078e3
Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ
diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000..c133a0c
Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ
diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
new file mode 100644
index 0000000..efc028a
Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ
diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..bfa42f0
Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ
diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
new file mode 100644
index 0000000..3af2608
Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ
diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..324e72c
Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ
diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
new file mode 100644
index 0000000..9bec2e6
Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ
diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..aee44e1
Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ
diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
new file mode 100644
index 0000000..34947cd
Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
new file mode 100644
index 0000000..3ab3e9c
--- /dev/null
+++ b/app/src/main/res/values/colors.xml
@@ -0,0 +1,6 @@
+
+
+ #3F51B5
+ #303F9F
+ #FF4081
+
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
new file mode 100644
index 0000000..c803474
--- /dev/null
+++ b/app/src/main/res/values/strings.xml
@@ -0,0 +1,3 @@
+
+ Zxing
+
diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml
new file mode 100644
index 0000000..5885930
--- /dev/null
+++ b/app/src/main/res/values/styles.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..c2eea8e
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,23 @@
+// Top-level build file where you can add configuration options common to all sub-projects/modules.
+
+buildscript {
+ repositories {
+ jcenter()
+ }
+ dependencies {
+ classpath 'com.android.tools.build:gradle:2.3.3'
+
+ // NOTE: Do not place your application dependencies here; they belong
+ // in the individual module build.gradle files
+ }
+}
+
+allprojects {
+ repositories {
+ jcenter()
+ }
+}
+
+task clean(type: Delete) {
+ delete rootProject.buildDir
+}
diff --git a/gradle.properties b/gradle.properties
new file mode 100644
index 0000000..aac7c9b
--- /dev/null
+++ b/gradle.properties
@@ -0,0 +1,17 @@
+# Project-wide Gradle settings.
+
+# IDE (e.g. Android Studio) users:
+# Gradle settings configured through the IDE *will override*
+# any settings specified in this file.
+
+# For more details on how to configure your build environment visit
+# http://www.gradle.org/docs/current/userguide/build_environment.html
+
+# Specifies the JVM arguments used for the daemon process.
+# The setting is particularly useful for tweaking memory settings.
+org.gradle.jvmargs=-Xmx1536m
+
+# When configured, Gradle will run in incubating parallel mode.
+# This option should only be used with decoupled projects. More details, visit
+# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
+# org.gradle.parallel=true
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..13372ae
Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..1f1d1b8
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,6 @@
+#Thu Jul 20 15:52:56 CST 2017
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
diff --git a/gradlew b/gradlew
new file mode 100644
index 0000000..9d82f78
--- /dev/null
+++ b/gradlew
@@ -0,0 +1,160 @@
+#!/usr/bin/env bash
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn ( ) {
+ echo "$*"
+}
+
+die ( ) {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+esac
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
+function splitJvmOpts() {
+ JVM_OPTS=("$@")
+}
+eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
+JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
+
+exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
diff --git a/gradlew.bat b/gradlew.bat
new file mode 100644
index 0000000..8a0b282
--- /dev/null
+++ b/gradlew.bat
@@ -0,0 +1,90 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windowz variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+if "%@eval[2+2]" == "4" goto 4NT_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+goto execute
+
+:4NT_args
+@rem Get arguments from the 4NT Shell from JP Software
+set CMD_LINE_ARGS=%$
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/settings.gradle b/settings.gradle
new file mode 100644
index 0000000..4e7dd9d
--- /dev/null
+++ b/settings.gradle
@@ -0,0 +1 @@
+include ':app', ':zxinglibrary'
diff --git a/zxinglibrary/.gitignore b/zxinglibrary/.gitignore
new file mode 100644
index 0000000..796b96d
--- /dev/null
+++ b/zxinglibrary/.gitignore
@@ -0,0 +1 @@
+/build
diff --git a/zxinglibrary/build.gradle b/zxinglibrary/build.gradle
new file mode 100644
index 0000000..baa48bb
--- /dev/null
+++ b/zxinglibrary/build.gradle
@@ -0,0 +1,31 @@
+apply plugin: 'com.android.library'
+
+android {
+ compileSdkVersion 26
+ buildToolsVersion "26.0.0"
+
+ defaultConfig {
+ minSdkVersion 15
+ targetSdkVersion 25
+ versionCode 1
+ versionName "1.0"
+
+ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+
+ }
+ buildTypes {
+ release {
+ minifyEnabled false
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+ }
+ }
+}
+
+dependencies {
+ compile fileTree(include: ['*.jar'], dir: 'libs')
+ androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
+ exclude group: 'com.android.support', module: 'support-annotations'
+ })
+ testCompile 'junit:junit:4.12'
+ compile files('libs/core-3.3.0.jar')
+}
diff --git a/zxinglibrary/libs/core-3.3.0.jar b/zxinglibrary/libs/core-3.3.0.jar
new file mode 100644
index 0000000..94f7406
Binary files /dev/null and b/zxinglibrary/libs/core-3.3.0.jar differ
diff --git a/zxinglibrary/proguard-rules.pro b/zxinglibrary/proguard-rules.pro
new file mode 100644
index 0000000..f8aec7c
--- /dev/null
+++ b/zxinglibrary/proguard-rules.pro
@@ -0,0 +1,25 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in D:\sdk/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the proguardFiles
+# directive in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# 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
diff --git a/zxinglibrary/src/androidTest/java/com/yzq/zxinglibrary/ExampleInstrumentedTest.java b/zxinglibrary/src/androidTest/java/com/yzq/zxinglibrary/ExampleInstrumentedTest.java
new file mode 100644
index 0000000..5955eff
--- /dev/null
+++ b/zxinglibrary/src/androidTest/java/com/yzq/zxinglibrary/ExampleInstrumentedTest.java
@@ -0,0 +1,26 @@
+package com.yzq.zxinglibrary;
+
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.*;
+
+/**
+ * Instrumentation test, which will execute on an Android device.
+ *
+ * @see Testing documentation
+ */
+@RunWith(AndroidJUnit4.class)
+public class ExampleInstrumentedTest {
+ @Test
+ public void useAppContext() throws Exception {
+ // Context of the app under test.
+ Context appContext = InstrumentationRegistry.getTargetContext();
+
+ assertEquals("com.yzq.zxinglibrary.test", appContext.getPackageName());
+ }
+}
diff --git a/zxinglibrary/src/main/AndroidManifest.xml b/zxinglibrary/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..9b36792
--- /dev/null
+++ b/zxinglibrary/src/main/AndroidManifest.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
+
+
diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/Consants.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/Consants.java
new file mode 100644
index 0000000..cb4a5ae
--- /dev/null
+++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/Consants.java
@@ -0,0 +1,18 @@
+package com.yzq.zxinglibrary;
+
+/**
+ * Created by yzq on 2017/7/20.
+ *
+ * zxing常量
+ */
+
+public class Consants {
+ public static final int DECODE = 1;
+ public static final int DECODE_FAILED = 2;
+ public static final int DECODE_SUCCEEDED = 3;
+ public static final int LAUNCH_PRODUCT_QUERY = 4;
+ public static final int QUIT = 5;
+ public static final int RESTART_PREVIEW = 6;
+ public static final int RETURN_SCAN_RESULT = 7;
+
+}
diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/BeepManager.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/BeepManager.java
new file mode 100644
index 0000000..fd97c3c
--- /dev/null
+++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/BeepManager.java
@@ -0,0 +1,169 @@
+/*
+ * 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.
+ */
+
+package com.yzq.zxinglibrary.android;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.res.AssetFileDescriptor;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.os.Vibrator;
+import android.preference.PreferenceManager;
+import android.util.Log;
+
+import com.yzq.zxinglibrary.R;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+
+/**
+ * Manages beeps and vibrations for {@link CaptureActivity}. 管理声音和震动
+ */
+public final class BeepManager implements MediaPlayer.OnCompletionListener,
+ MediaPlayer.OnErrorListener, Closeable {
+
+ private static final String TAG = BeepManager.class.getSimpleName();
+
+ private static final float BEEP_VOLUME = 0.10f;
+ private static final long VIBRATE_DURATION = 200L;
+
+ private final Activity activity;
+ private MediaPlayer mediaPlayer;
+ private boolean playBeep;
+ private boolean vibrate;
+
+ public BeepManager(Activity activity) {
+ this.activity = activity;
+ this.mediaPlayer = null;
+ updatePrefs();
+ }
+
+ public synchronized void updatePrefs() {
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(activity);
+ playBeep = shouldBeep(prefs, activity);
+ vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false);
+ if (playBeep && mediaPlayer == null) {
+ // The volume on STREAM_SYSTEM is not adjustable, and users found it
+ // too loud,
+ // so we now play on the music stream.
+ // 设置activity音量控制键控制的音频流
+ activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
+ mediaPlayer = buildMediaPlayer(activity);
+ }
+ }
+
+ /**
+ * 开启响铃和震动
+ */
+ public synchronized void playBeepSoundAndVibrate() {
+ if (playBeep && mediaPlayer != null) {
+ mediaPlayer.start();
+ }
+ if (vibrate) {
+ Vibrator vibrator = (Vibrator) activity
+ .getSystemService(Context.VIBRATOR_SERVICE);
+ vibrator.vibrate(VIBRATE_DURATION);
+ }
+ }
+
+ /**
+ * 判断是否需要响铃
+ *
+ * @param prefs
+ * @param activity
+ * @return
+ */
+ private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
+ boolean shouldPlayBeep = prefs.getBoolean(
+ PreferencesActivity.KEY_PLAY_BEEP, true);
+ if (shouldPlayBeep) {
+ // See if sound settings overrides this
+ AudioManager audioService = (AudioManager) activity
+ .getSystemService(Context.AUDIO_SERVICE);
+ if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
+ shouldPlayBeep = false;
+ }
+ }
+ return shouldPlayBeep;
+ }
+
+ /**
+ * 创建MediaPlayer
+ *
+ * @param activity
+ * @return
+ */
+ private MediaPlayer buildMediaPlayer(Context activity) {
+ MediaPlayer mediaPlayer = new MediaPlayer();
+ mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
+ // 监听是否播放完成
+ mediaPlayer.setOnCompletionListener(this);
+ mediaPlayer.setOnErrorListener(this);
+ // 配置播放资源
+ try {
+ AssetFileDescriptor file = activity.getResources()
+ .openRawResourceFd(R.raw.beep);
+ try {
+ mediaPlayer.setDataSource(file.getFileDescriptor(),
+ file.getStartOffset(), file.getLength());
+ } finally {
+ file.close();
+ }
+ // 设置音量
+ mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
+ mediaPlayer.prepare();
+ return mediaPlayer;
+ } catch (IOException ioe) {
+ Log.w(TAG, ioe);
+ mediaPlayer.release();
+ return null;
+ }
+ }
+
+ @Override
+ public void onCompletion(MediaPlayer mp) {
+ // When the beep has finished playing, rewind to queue up another one.
+ mp.seekTo(0);
+ }
+
+ @Override
+ public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
+ if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
+ // we are finished, so put up an appropriate error toast if required
+ // and finish
+ activity.finish();
+ } else {
+ // possibly media player error, so release and recreate
+ mp.release();
+ mediaPlayer = null;
+ updatePrefs();
+ }
+ return true;
+ }
+
+ @Override
+ public synchronized void close() {
+ if (mediaPlayer != null) {
+ mediaPlayer.release();
+ mediaPlayer = null;
+ }
+ }
+
+}
diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/CaptureActivity.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/CaptureActivity.java
new file mode 100644
index 0000000..c258897
--- /dev/null
+++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/CaptureActivity.java
@@ -0,0 +1,239 @@
+package com.yzq.zxinglibrary.android;
+
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.Intent;
+import android.graphics.Bitmap;
+import android.os.Bundle;
+import android.os.Handler;
+import android.util.Log;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.View;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.ImageButton;
+import android.widget.Toast;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.DecodeHintType;
+import com.google.zxing.Result;
+import com.yzq.zxinglibrary.R;
+import com.yzq.zxinglibrary.camera.CameraManager;
+import com.yzq.zxinglibrary.view.ViewfinderView;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * 这个activity打开相机,在后台线程做常规的扫描;它绘制了一个结果view来帮助正确地显示条形码,在扫描的时候显示反馈信息,
+ * 然后在扫描成功的时候覆盖扫描结果
+ */
+public final class CaptureActivity extends Activity implements
+ SurfaceHolder.Callback {
+
+ private static final String TAG = CaptureActivity.class.getSimpleName();
+
+ // 相机控制
+ private CameraManager cameraManager;
+ private CaptureActivityHandler handler;
+ private ViewfinderView viewfinderView;
+ private boolean hasSurface;
+ private IntentSource source;
+ private Collection decodeFormats;
+ private Map decodeHints;
+ private String characterSet;
+ // 电量控制
+ private InactivityTimer inactivityTimer;
+ // 声音、震动控制
+ private BeepManager beepManager;
+
+ private ImageButton imageButton_back;
+
+ public ViewfinderView getViewfinderView() {
+ return viewfinderView;
+ }
+
+ public Handler getHandler() {
+ return handler;
+ }
+
+ public CameraManager getCameraManager() {
+ return cameraManager;
+ }
+
+ public void drawViewfinder() {
+ viewfinderView.drawViewfinder();
+ }
+
+ /**
+ * OnCreate中初始化一些辅助类,如InactivityTimer(休眠)、Beep(声音)以及AmbientLight(闪光灯)
+ */
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ // 保持Activity处于唤醒状态
+ Window window = getWindow();
+ window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+ setContentView(R.layout.capture);
+
+ hasSurface = false;
+
+ inactivityTimer = new InactivityTimer(this);
+ beepManager = new BeepManager(this);
+
+ imageButton_back = (ImageButton) findViewById(R.id.capture_imageview_back);
+ imageButton_back.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ finish();
+ }
+ });
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ // CameraManager必须在这里初始化,而不是在onCreate()中。
+ // 这是必须的,因为当我们第一次进入时需要显示帮助页,我们并不想打开Camera,测量屏幕大小
+ // 当扫描框的尺寸不正确时会出现bug
+ cameraManager = new CameraManager(getApplication());
+
+ viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
+ viewfinderView.setCameraManager(cameraManager);
+
+ handler = null;
+
+ SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
+ SurfaceHolder surfaceHolder = surfaceView.getHolder();
+ if (hasSurface) {
+ // activity在paused时但不会stopped,因此surface仍旧存在;
+ // surfaceCreated()不会调用,因此在这里初始化camera
+ initCamera(surfaceHolder);
+ } else {
+ // 重置callback,等待surfaceCreated()来初始化camera
+ surfaceHolder.addCallback(this);
+ }
+
+ beepManager.updatePrefs();
+ inactivityTimer.onResume();
+
+ source = IntentSource.NONE;
+ decodeFormats = null;
+ characterSet = null;
+ }
+
+ @Override
+ protected void onPause() {
+ if (handler != null) {
+ handler.quitSynchronously();
+ handler = null;
+ }
+ inactivityTimer.onPause();
+ beepManager.close();
+ cameraManager.closeDriver();
+ if (!hasSurface) {
+ SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
+ SurfaceHolder surfaceHolder = surfaceView.getHolder();
+ surfaceHolder.removeCallback(this);
+ }
+ super.onPause();
+ }
+
+ @Override
+ protected void onDestroy() {
+ inactivityTimer.shutdown();
+ super.onDestroy();
+ }
+
+ @Override
+ public void surfaceCreated(SurfaceHolder holder) {
+ if (!hasSurface) {
+ hasSurface = true;
+ initCamera(holder);
+ }
+ }
+
+ @Override
+ public void surfaceDestroyed(SurfaceHolder holder) {
+ hasSurface = false;
+ }
+
+ @Override
+ public void surfaceChanged(SurfaceHolder holder, int format, int width,
+ int height) {
+
+ }
+
+ /**
+ * 扫描成功,处理反馈信息
+ *
+ * @param rawResult
+ * @param barcode
+ * @param scaleFactor
+ */
+ public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
+ inactivityTimer.onActivity();
+
+ boolean fromLiveScan = barcode != null;
+ //这里处理解码完成后的结果,此处将参数回传到Activity处理
+ if (fromLiveScan) {
+ beepManager.playBeepSoundAndVibrate();
+
+ Toast.makeText(this, "扫描成功", Toast.LENGTH_SHORT).show();
+
+ Intent intent = getIntent();
+ intent.putExtra("codedContent", rawResult.getText());
+ intent.putExtra("codedBitmap", barcode);
+ setResult(RESULT_OK, intent);
+ finish();
+ }
+
+ }
+
+ /**
+ * 初始化Camera
+ *
+ * @param surfaceHolder
+ */
+ private void initCamera(SurfaceHolder surfaceHolder) {
+ if (surfaceHolder == null) {
+ throw new IllegalStateException("No SurfaceHolder provided");
+ }
+ if (cameraManager.isOpen()) {
+ return;
+ }
+ try {
+ // 打开Camera硬件设备
+ cameraManager.openDriver(surfaceHolder);
+ // 创建一个handler来打开预览,并抛出一个运行时异常
+ if (handler == null) {
+ handler = new CaptureActivityHandler(this, decodeFormats,
+ decodeHints, characterSet, cameraManager);
+ }
+ } catch (IOException ioe) {
+ Log.w(TAG, ioe);
+ displayFrameworkBugMessageAndExit();
+ } catch (RuntimeException e) {
+ Log.w(TAG, "Unexpected error initializing camera", e);
+ displayFrameworkBugMessageAndExit();
+ }
+ }
+
+ /**
+ * 显示底层错误信息并退出应用
+ */
+ private void displayFrameworkBugMessageAndExit() {
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setTitle("扫一扫");
+ builder.setMessage(getString(R.string.msg_camera_framework_bug));
+ builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
+ builder.setOnCancelListener(new FinishListener(this));
+ builder.show();
+ }
+
+}
diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/CaptureActivityHandler.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/CaptureActivityHandler.java
new file mode 100644
index 0000000..f0c89da
--- /dev/null
+++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/CaptureActivityHandler.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+package com.yzq.zxinglibrary.android;
+
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.provider.Browser;
+import android.util.Log;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.DecodeHintType;
+import com.google.zxing.Result;
+import com.yzq.zxinglibrary.Consants;
+import com.yzq.zxinglibrary.camera.CameraManager;
+import com.yzq.zxinglibrary.decode.DecodeThread;
+import com.yzq.zxinglibrary.view.ViewfinderResultPointCallback;
+
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * This class handles all the messaging which comprises the state machine for
+ * capture. 该类用于处理有关拍摄状态的所有信息
+ *
+ * @author dswitkin@google.com (Daniel Switkin)
+ */
+public final class CaptureActivityHandler extends Handler {
+
+ private static final String TAG = CaptureActivityHandler.class
+ .getSimpleName();
+
+ private final CaptureActivity activity;
+ private final DecodeThread decodeThread;
+ private State state;
+ private final CameraManager cameraManager;
+
+ private enum State {
+ PREVIEW, SUCCESS, DONE
+ }
+
+ public CaptureActivityHandler(CaptureActivity activity,
+ Collection decodeFormats,
+ Map baseHints, String characterSet,
+ CameraManager cameraManager) {
+ this.activity = activity;
+ decodeThread = new DecodeThread(activity, decodeFormats, baseHints,
+ characterSet, new ViewfinderResultPointCallback(
+ activity.getViewfinderView()));
+ decodeThread.start();
+ state = State.SUCCESS;
+
+ // Start ourselves capturing previews and decoding.
+ // 开始拍摄预览和解码
+ this.cameraManager = cameraManager;
+ cameraManager.startPreview();
+ restartPreviewAndDecode();
+ }
+
+ @Override
+ public void handleMessage(Message message) {
+ switch (message.what) {
+ case Consants.RESTART_PREVIEW:
+ // 重新预览
+ restartPreviewAndDecode();
+ break;
+ case Consants.DECODE_SUCCEEDED:
+ // 解码成功
+ state = State.SUCCESS;
+ Bundle bundle = message.getData();
+ Bitmap barcode = null;
+ float scaleFactor = 1.0f;
+ if (bundle != null) {
+ byte[] compressedBitmap = bundle
+ .getByteArray(DecodeThread.BARCODE_BITMAP);
+ if (compressedBitmap != null) {
+ barcode = BitmapFactory.decodeByteArray(compressedBitmap,
+ 0, compressedBitmap.length, null);
+ // Mutable copy:
+ barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
+ }
+ scaleFactor = bundle
+ .getFloat(DecodeThread.BARCODE_SCALED_FACTOR);
+ }
+ activity.handleDecode((Result) message.obj, barcode, scaleFactor);
+ break;
+ case Consants.DECODE_FAILED:
+ // We're decoding as fast as possible, so when one decode fails,
+ // start another.
+ // 尽可能快的解码,以便可以在解码失败时,开始另一次解码
+ state = State.PREVIEW;
+ cameraManager.requestPreviewFrame(decodeThread.getHandler(),
+ Consants.DECODE);
+ break;
+ case Consants.RETURN_SCAN_RESULT:
+ //扫描结果,返回CaptureActivity处理
+ activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
+ activity.finish();
+ break;
+ case Consants.LAUNCH_PRODUCT_QUERY:
+ String url = (String) message.obj;
+
+ Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+ intent.setData(Uri.parse(url));
+
+ ResolveInfo resolveInfo = activity.getPackageManager()
+ .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
+ String browserPackageName = null;
+ if (resolveInfo != null && resolveInfo.activityInfo != null) {
+ browserPackageName = resolveInfo.activityInfo.packageName;
+ Log.d(TAG, "Using browser in package " + browserPackageName);
+ }
+
+ // Needed for default Android browser / Chrome only apparently
+ //需要默认的Android浏览器或者Google
+ if ("com.android.browser".equals(browserPackageName)
+ || "com.android.chrome".equals(browserPackageName)) {
+ intent.setPackage(browserPackageName);
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ intent.putExtra(Browser.EXTRA_APPLICATION_ID,
+ browserPackageName);
+ }
+
+ try {
+ activity.startActivity(intent);
+ } catch (ActivityNotFoundException ignored) {
+ Log.w(TAG, "Can't find anything to handle VIEW of URI " + url);
+ }
+ break;
+ }
+ }
+
+ /**
+ * 完全退出
+ */
+ public void quitSynchronously() {
+ state = State.DONE;
+ cameraManager.stopPreview();
+ Message quit = Message.obtain(decodeThread.getHandler(), Consants.QUIT);
+ quit.sendToTarget();
+ try {
+ // Wait at most half a second; should be enough time, and onPause()
+ // will timeout quickly
+ decodeThread.join(500L);
+ } catch (InterruptedException e) {
+ // continue
+ }
+
+ // Be absolutely sure we don't send any queued up messages
+ //确保不会发送任何队列消息
+ removeMessages(Consants.DECODE_SUCCEEDED);
+ removeMessages(Consants.DECODE_FAILED);
+ }
+
+ public void restartPreviewAndDecode() {
+ if (state == State.SUCCESS) {
+ state = State.PREVIEW;
+ cameraManager.requestPreviewFrame(decodeThread.getHandler(),
+ Consants.DECODE);
+ activity.drawViewfinder();
+ }
+ }
+
+}
diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/FinishListener.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/FinishListener.java
new file mode 100644
index 0000000..4e09445
--- /dev/null
+++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/FinishListener.java
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+package com.yzq.zxinglibrary.android;
+
+import android.app.Activity;
+import android.content.DialogInterface;
+
+/**
+ * Simple listener used to exit the app in a few cases.
+ * 用于在少数情况下退出App的监听
+ *
+ * @author Sean Owen
+ */
+public final class FinishListener implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
+
+ private final Activity activityToFinish;
+
+ public FinishListener(Activity activityToFinish) {
+ this.activityToFinish = activityToFinish;
+ }
+
+ @Override
+ public void onCancel(DialogInterface dialogInterface) {
+ run();
+ }
+
+ @Override
+ public void onClick(DialogInterface dialogInterface, int i) {
+ run();
+ }
+
+ private void run() {
+ activityToFinish.finish();
+ }
+
+}
diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/InactivityTimer.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/InactivityTimer.java
new file mode 100644
index 0000000..8efb0d7
--- /dev/null
+++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/android/InactivityTimer.java
@@ -0,0 +1,136 @@
+/*
+ * 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.
+ */
+
+package com.yzq.zxinglibrary.android;
+
+import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.AsyncTask;
+import android.os.BatteryManager;
+import android.util.Log;
+
+/**
+ *
+ * Finishes an activity after a period of inactivity if the device is on battery
+ * power. 如果设备使用电池供电,一段时间不活动之后结束activity
+ *
+ * 供电控制
+ */
+public final class InactivityTimer {
+
+ private static final String TAG = InactivityTimer.class.getSimpleName();
+
+ private static final long INACTIVITY_DELAY_MS = 5 * 60 * 1000L;
+
+ private final Activity activity;
+ private final BroadcastReceiver powerStatusReceiver;
+ private boolean registered;
+ private AsyncTask