diff --git a/sample/src/main/java/example/zxing/CustomScannerActivity.java b/sample/src/main/java/example/zxing/CustomScannerActivity.java index ef41f1c56..df3a7099b 100644 --- a/sample/src/main/java/example/zxing/CustomScannerActivity.java +++ b/sample/src/main/java/example/zxing/CustomScannerActivity.java @@ -12,9 +12,6 @@ import com.journeyapps.barcodescanner.CaptureManager; import com.journeyapps.barcodescanner.DecoratedBarcodeView; -import com.journeyapps.barcodescanner.ViewfinderView; - -import java.util.Random; /** * Custom Scannner Activity extending from Activity to display a custom layout form scanner view. @@ -25,7 +22,6 @@ public class CustomScannerActivity extends Activity implements private CaptureManager capture; private DecoratedBarcodeView barcodeScannerView; private Button switchFlashlightButton; - private ViewfinderView viewfinderView; @Override protected void onCreate(Bundle savedInstanceState) { @@ -37,8 +33,6 @@ protected void onCreate(Bundle savedInstanceState) { switchFlashlightButton = findViewById(R.id.switch_flashlight); - viewfinderView = findViewById(R.id.zxing_viewfinder_view); - // if the device does not have flashlight in its camera, // then remove the switch flashlight button... if (!hasFlash()) { @@ -49,9 +43,6 @@ protected void onCreate(Bundle savedInstanceState) { capture.initializeFromIntent(getIntent(), savedInstanceState); capture.setShowMissingCameraPermissionDialog(false); capture.decode(); - - changeMaskColor(null); - changeLaserVisibility(true); } @Override @@ -100,16 +91,6 @@ public void switchFlashlight(View view) { } } - public void changeMaskColor(View view) { - Random rnd = new Random(); - int color = Color.argb(100, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); - viewfinderView.setMaskColor(color); - } - - public void changeLaserVisibility(boolean visible) { - viewfinderView.setLaserVisibility(visible); - } - @Override public void onTorchOn() { switchFlashlightButton.setText(R.string.turn_off_flashlight); diff --git a/sample/src/main/res/layout/custom_barcode_scanner.xml b/sample/src/main/res/layout/custom_barcode_scanner.xml index 8607280f7..6b2e23d69 100644 --- a/sample/src/main/res/layout/custom_barcode_scanner.xml +++ b/sample/src/main/res/layout/custom_barcode_scanner.xml @@ -1,7 +1,7 @@ + xmlns:tools="http://schemas.android.com/tools" + xmlns:app="http://schemas.android.com/apk/res-auto"> + app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" /> \(.*\)$'` + 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 + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# 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 +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +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" -a "$nonstop" = "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 + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/zxing-android-embedded/gradlew.bat b/zxing-android-embedded/gradlew.bat new file mode 100644 index 000000000..e95643d6a --- /dev/null +++ b/zxing-android-embedded/gradlew.bat @@ -0,0 +1,84 @@ +@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 + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@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= + +@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 Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_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=%* + +: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/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java b/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java index cdaeb9aa1..d227f82ea 100755 --- a/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java +++ b/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java @@ -54,16 +54,15 @@ public class ViewfinderView extends View { protected final int resultColor; protected final int laserColor; protected final int resultPointColor; - protected boolean laserVisibility; protected int scannerAlpha; protected List possibleResultPoints; protected List lastPossibleResultPoints; protected CameraPreview cameraPreview; - // Cache the framingRect and previewSize, so that we can still draw it after the preview + // Cache the framingRect and previewFramingRect, so that we can still draw it after the preview // stopped. protected Rect framingRect; - protected Size previewSize; + protected Rect previewFramingRect; // This constructor is used when the class is built from an XML resource. public ViewfinderView(Context context, AttributeSet attrs) { @@ -71,9 +70,9 @@ public ViewfinderView(Context context, AttributeSet attrs) { // Initialize these once for performance rather than calling them every time in onDraw(). paint = new Paint(Paint.ANTI_ALIAS_FLAG); + paint.setColor(getResources().getColor(android.R.color.white)); Resources resources = getResources(); - // Get setted attributes on view TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.zxing_finder); @@ -82,11 +81,9 @@ public ViewfinderView(Context context, AttributeSet attrs) { this.resultColor = attributes.getColor(R.styleable.zxing_finder_zxing_result_view, resources.getColor(R.color.zxing_result_view)); this.laserColor = attributes.getColor(R.styleable.zxing_finder_zxing_viewfinder_laser, - resources.getColor(R.color.zxing_viewfinder_laser)); + resources.getColor(R.color.zxing_transparent)); this.resultPointColor = attributes.getColor(R.styleable.zxing_finder_zxing_possible_result_points, resources.getColor(R.color.zxing_possible_result_points)); - this.laserVisibility = attributes.getBoolean(R.styleable.zxing_finder_zxing_viewfinder_laser_visibility, - true); attributes.recycle(); @@ -131,84 +128,81 @@ protected void refreshSizes() { return; } Rect framingRect = cameraPreview.getFramingRect(); - Size previewSize = cameraPreview.getPreviewSize(); - if (framingRect != null && previewSize != null) { + Rect previewFramingRect = cameraPreview.getPreviewFramingRect(); + if (framingRect != null && previewFramingRect != null) { this.framingRect = framingRect; - this.previewSize = previewSize; + this.previewFramingRect = previewFramingRect; } } + + @SuppressLint("DrawAllocation") @Override public void onDraw(Canvas canvas) { refreshSizes(); - if (framingRect == null || previewSize == null) { + if (framingRect == null || previewFramingRect == null) { return; } - final Rect frame = framingRect; - final Size previewSize = this.previewSize; + Rect frame = framingRect; + Rect previewFrame = previewFramingRect; + + //inside onDraw + int distance = (frame.bottom - frame.top) / 4; + int thickness = 15; + + //top left corner + canvas.drawRect(frame.left - thickness, frame.top - thickness, distance + frame.left, frame.top, paint); + canvas.drawRect(frame.left - thickness, frame.top, frame.left, distance + frame.top, paint); + + //top right corner + canvas.drawRect(frame.right - distance, frame.top - thickness, frame.right + thickness, frame.top, paint); + canvas.drawRect(frame.right, frame.top, frame.right + thickness, distance + frame.top, paint); + + //bottom left corner + canvas.drawRect(frame.left - thickness, frame.bottom, distance + frame.left, frame.bottom + thickness, paint); + canvas.drawRect(frame.left - thickness, frame.bottom - distance, frame.left, frame.bottom, paint); - final int width = canvas.getWidth(); - final int height = canvas.getHeight(); + //bottom right corner + canvas.drawRect(frame.right - distance, frame.bottom, frame.right + thickness, frame.bottom + thickness, paint); + canvas.drawRect(frame.right, frame.bottom - distance, frame.right + thickness, frame.bottom, paint); - // Draw the exterior (i.e. outside the framing rect) darkened - paint.setColor(resultBitmap != null ? resultColor : maskColor); - canvas.drawRect(0, 0, width, frame.top, paint); - canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); - canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); - canvas.drawRect(0, frame.bottom + 1, width, height, paint); if (resultBitmap != null) { // Draw the opaque result bitmap over the scanning rectangle paint.setAlpha(CURRENT_POINT_OPACITY); canvas.drawBitmap(resultBitmap, null, frame, paint); } else { - // If wanted, draw a red "laser scanner" line through the middle to show decoding is active - if (laserVisibility) { - paint.setColor(laserColor); - paint.setAlpha(SCANNER_ALPHA[scannerAlpha]); - scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length; - - final int middle = frame.height() / 2 + frame.top; - canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint); - } - - final float scaleX = this.getWidth() / (float) previewSize.width; - final float scaleY = this.getHeight() / (float) previewSize.height; - - // draw the last possible result points - if (!lastPossibleResultPoints.isEmpty()) { - paint.setAlpha(CURRENT_POINT_OPACITY / 2); + float scaleX = frame.width() / (float) previewFrame.width(); + float scaleY = frame.height() / (float) previewFrame.height(); + + List currentPossible = possibleResultPoints; + List currentLast = lastPossibleResultPoints; + int frameLeft = frame.left; + int frameTop = frame.top; + if (currentPossible.isEmpty()) { + lastPossibleResultPoints = null; + } else { + possibleResultPoints = new ArrayList<>(5); + lastPossibleResultPoints = currentPossible; + paint.setAlpha(CURRENT_POINT_OPACITY); paint.setColor(resultPointColor); - float radius = POINT_SIZE / 2.0f; - for (final ResultPoint point : lastPossibleResultPoints) { - canvas.drawCircle( - (int) (point.getX() * scaleX), - (int) (point.getY() * scaleY), - radius, paint - ); + for (ResultPoint point : currentPossible) { + canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), + frameTop + (int) (point.getY() * scaleY), + POINT_SIZE, paint); } - lastPossibleResultPoints.clear(); } - - // draw current possible result points - if (!possibleResultPoints.isEmpty()) { - paint.setAlpha(CURRENT_POINT_OPACITY); + if (currentLast != null) { + paint.setAlpha(CURRENT_POINT_OPACITY / 2); paint.setColor(resultPointColor); - for (final ResultPoint point : possibleResultPoints) { - canvas.drawCircle( - (int) (point.getX() * scaleX), - (int) (point.getY() * scaleY), - POINT_SIZE, paint - ); + float radius = POINT_SIZE / 2.0f; + for (ResultPoint point : currentLast) { + canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), + frameTop + (int) (point.getY() * scaleY), + radius, paint); } - - // swap and clear buffers - final List temp = possibleResultPoints; - possibleResultPoints = lastPossibleResultPoints; - lastPossibleResultPoints = temp; - possibleResultPoints.clear(); } // Request another update at the animation interval, but only repaint the laser line, @@ -246,15 +240,12 @@ public void drawResultBitmap(Bitmap result) { * @param point a point to draw, relative to the preview frame */ public void addPossibleResultPoint(ResultPoint point) { - if (possibleResultPoints.size() < MAX_RESULT_POINTS) - possibleResultPoints.add(point); - } - - public void setMaskColor(int maskColor) { - this.maskColor = maskColor; - } - - public void setLaserVisibility(boolean visible) { - this.laserVisibility = visible; + List points = possibleResultPoints; + points.add(point); + int size = points.size(); + if (size > MAX_RESULT_POINTS) { + // trim it + points.subList(0, size - MAX_RESULT_POINTS / 2).clear(); + } } }