diff --git a/README.md b/README.md index 9a1fd3c..776f94d 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@ ![Image](app/src/main/ic_launcher-web.png) [![Download](https://img.shields.io/badge/download-App-blue.svg)](https://raw.githubusercontent.com/jenly1314/ZXingLite/master/app/release/app-release.apk) -[![JCenter](https://img.shields.io/badge/JCenter-1.1.7-46C018.svg)](https://bintray.com/beta/#/jenly/maven/zxing-lite) +[![JCenter](https://img.shields.io/badge/JCenter-1.1.8-46C018.svg)](https://bintray.com/beta/#/jenly/maven/zxing-lite) [![JitPack](https://jitpack.io/v/jenly1314/ZXingLite.svg)](https://jitpack.io/#jenly1314/ZXingLite) [![CI](https://travis-ci.org/jenly1314/ZXingLite.svg?branch=master)](https://travis-ci.org/jenly1314/ZXingLite) [![CircleCI](https://circleci.com/gh/jenly1314/ZXingLite.svg?style=svg)](https://circleci.com/gh/jenly1314/ZXingLite) [![API](https://img.shields.io/badge/API-16%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=16) [![License](https://img.shields.io/badge/license-Apche%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0) [![Blog](https://img.shields.io/badge/blog-Jenly-9933CC.svg)](https://jenly1314.github.io/) -[![QQGroup](https://img.shields.io/badge/QQGroup-20867961-blue.svg)](http://shang.qq.com/wpa/qunwpa?idkey=8fcc6a2f88552ea44b1.1.782c94fd124f7bb3ec227e2a400dbbfaad3dc2f5ad) +[![QQGroup](https://img.shields.io/badge/QQGroup-20867961-blue.svg)](http://shang.qq.com/wpa/qunwpa?idkey=8fcc6a2f88552ea44b1.1.882c94fd124f7bb3ec227e2a400dbbfaad3dc2f5ad) ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成二维码/条形码功能,扫码界面完全支持自定义,也可一行代码使用默认实现的扫码功能。总之你想要的都在这里。 >简单如斯,你不试试? Come on~ @@ -55,21 +55,21 @@ ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成 com.king.zxing zxing-lite - 1.1.7 + 1.1.8 pom ``` ### Gradle: ```gradle //AndroidX 版本 -implementation 'com.king.zxing:zxing-lite:1.1.7-androidx' +implementation 'com.king.zxing:zxing-lite:1.1.8-androidx' //Android 版本 -implementation 'com.king.zxing:zxing-lite:1.1.7' +implementation 'com.king.zxing:zxing-lite:1.1.8' ``` ### Lvy: ```lvy - + ``` @@ -170,6 +170,10 @@ api 'com.google.zxing:core:3.3.3' ## 版本记录 +#### v1.1.8:2020-4-27 +* 统一日志管理 +* 优化细节 + #### v1.1.7:2020-3-29 * 优化一些默认参数配置 * 修复扫码界面开启闪光灯并切到后台时,手电筒按钮状态未同步问题(fix#81) @@ -249,7 +253,7 @@ api 'com.google.zxing:core:3.3.3' Github: jenly1314 - 加入QQ群: 20867961 + 加入QQ群: 20867961
diff --git a/app/release/app-release.apk b/app/release/app-release.apk index 6c39367..062ad4d 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/release/output.json b/app/release/output.json index 9bf3e43..c6bf638 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":22,"versionName":"1.1.7-androidx","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":24,"versionName":"1.1.8-androidx","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/lib/src/main/java/com/king/zxing/BeepManager.java b/lib/src/main/java/com/king/zxing/BeepManager.java index 76d0fd9..52d776d 100644 --- a/lib/src/main/java/com/king/zxing/BeepManager.java +++ b/lib/src/main/java/com/king/zxing/BeepManager.java @@ -26,7 +26,8 @@ import android.media.MediaPlayer; import android.os.Build; import android.os.Vibrator; import android.preference.PreferenceManager; -import android.util.Log; + +import com.king.zxing.util.LogUtils; import java.io.Closeable; import java.io.IOException; @@ -106,7 +107,7 @@ public final class BeepManager implements MediaPlayer.OnErrorListener, Closeable mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { - Log.w(TAG, ioe); + LogUtils.w(ioe); mediaPlayer.release(); return null; } diff --git a/lib/src/main/java/com/king/zxing/CaptureActivity.java b/lib/src/main/java/com/king/zxing/CaptureActivity.java index 65ec483..0f4ca83 100644 --- a/lib/src/main/java/com/king/zxing/CaptureActivity.java +++ b/lib/src/main/java/com/king/zxing/CaptureActivity.java @@ -47,6 +47,7 @@ public class CaptureActivity extends AppCompatActivity implements OnCaptureCallb setContentView(layoutId); } initUI(); + mCaptureHelper.onCreate(); } /** @@ -60,9 +61,12 @@ public class CaptureActivity extends AppCompatActivity implements OnCaptureCallb ivTorch = findViewById(ivTorchId); ivTorch.setVisibility(View.INVISIBLE); } + initCaptureHelper(); + } + + public void initCaptureHelper(){ mCaptureHelper = new CaptureHelper(this,surfaceView,viewfinderView,ivTorch); mCaptureHelper.setOnCaptureCallback(this); - mCaptureHelper.onCreate(); } /** @@ -157,4 +161,4 @@ public class CaptureActivity extends AppCompatActivity implements OnCaptureCallb public boolean onResultCallback(String result) { return false; } -} +} \ No newline at end of file diff --git a/lib/src/main/java/com/king/zxing/CaptureFragment.java b/lib/src/main/java/com/king/zxing/CaptureFragment.java index 7d3f31e..e44793e 100644 --- a/lib/src/main/java/com/king/zxing/CaptureFragment.java +++ b/lib/src/main/java/com/king/zxing/CaptureFragment.java @@ -73,6 +73,10 @@ public class CaptureFragment extends Fragment implements OnCaptureCallback { ivTorch = mRootView.findViewById(ivTorchId); ivTorch.setVisibility(View.INVISIBLE); } + initCaptureHelper(); + } + + public void initCaptureHelper(){ mCaptureHelper = new CaptureHelper(this,surfaceView,viewfinderView,ivTorch); mCaptureHelper.setOnCaptureCallback(this); } diff --git a/lib/src/main/java/com/king/zxing/CaptureHelper.java b/lib/src/main/java/com/king/zxing/CaptureHelper.java index d249ffe..8e5be52 100644 --- a/lib/src/main/java/com/king/zxing/CaptureHelper.java +++ b/lib/src/main/java/com/king/zxing/CaptureHelper.java @@ -33,6 +33,7 @@ import com.google.zxing.DecodeHintType; import com.google.zxing.Result; import com.king.zxing.camera.CameraManager; import com.king.zxing.camera.FrontLightMode; +import com.king.zxing.util.LogUtils; import java.io.IOException; import java.util.ArrayList; @@ -44,13 +45,14 @@ import java.util.Map; import androidx.annotation.FloatRange; import androidx.fragment.app.Fragment; +/** + * @author Jenly + */ /** * @author Jenly */ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,CaptureManager, SurfaceHolder.Callback { - public static final String TAG = CaptureHelper.class.getSimpleName(); - private Activity activity; private CaptureHandler captureHandler; @@ -63,6 +65,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture private AmbientLightManager ambientLightManager; + private SurfaceView surfaceView; private ViewfinderView viewfinderView; private SurfaceHolder surfaceHolder; private View ivTorch; @@ -186,15 +189,16 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture */ public CaptureHelper(Activity activity,SurfaceView surfaceView,ViewfinderView viewfinderView,View ivTorch){ this.activity = activity; + this.surfaceView = surfaceView; this.viewfinderView = viewfinderView; this.ivTorch = ivTorch; - surfaceHolder = surfaceView.getHolder(); - hasSurface = false; } @Override public void onCreate(){ + surfaceHolder = surfaceView.getHolder(); + hasSurface = false; inactivityTimer = new InactivityTimer(activity); beepManager = new BeepManager(activity); ambientLightManager = new AmbientLightManager(activity); @@ -238,7 +242,6 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture public void onPause(){ if (captureHandler != null) { captureHandler.quitSynchronously(); - captureHandler = null; } inactivityTimer.onPause(); ambientLightManager.stop(); @@ -332,7 +335,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture throw new IllegalStateException("No SurfaceHolder provided"); } if (cameraManager.isOpen()) { - Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?"); + LogUtils.w("initCamera() while already open -- late SurfaceView callback?"); return; } try { @@ -346,18 +349,18 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture captureHandler.setSupportLuminanceInvert(isSupportLuminanceInvert); } } catch (IOException ioe) { - Log.w(TAG, ioe); + LogUtils.w(ioe); } catch (RuntimeException e) { // Barcode Scanner has seen crashes in the wild of this variety: // java.?lang.?RuntimeException: Fail to connect to camera service - Log.w(TAG, "Unexpected error initializing camera", e); + LogUtils.w( "Unexpected error initializing camera", e); } } @Override public void surfaceCreated(SurfaceHolder holder) { if (holder == null) { - Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!"); + LogUtils.w( "*** WARNING *** surfaceCreated() gave us a null surface!"); } if (!hasSurface) { hasSurface = true; @@ -393,7 +396,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture params.setZoom(zoom); camera.setParameters(params); } else { - Log.i(TAG, "zoom not supported"); + LogUtils.i( "zoom not supported"); } } diff --git a/lib/src/main/java/com/king/zxing/DecodeHandler.java b/lib/src/main/java/com/king/zxing/DecodeHandler.java index f0fd063..9659185 100644 --- a/lib/src/main/java/com/king/zxing/DecodeHandler.java +++ b/lib/src/main/java/com/king/zxing/DecodeHandler.java @@ -19,13 +19,11 @@ package com.king.zxing; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Point; -import android.graphics.PointF; import android.hardware.Camera; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.util.Log; import android.view.Display; import android.view.WindowManager; @@ -33,22 +31,19 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.MultiFormatReader; -import com.google.zxing.NotFoundException; import com.google.zxing.PlanarYUVLuminanceSource; -import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.ResultPoint; import com.google.zxing.common.GlobalHistogramBinarizer; import com.google.zxing.common.HybridBinarizer; import com.king.zxing.camera.CameraManager; +import com.king.zxing.util.LogUtils; import java.io.ByteArrayOutputStream; import java.util.Map; final class DecodeHandler extends Handler { - private static final String TAG = DecodeHandler.class.getSimpleName(); - private final Context context; private final CameraManager cameraManager; private final CaptureHandler handler; @@ -151,7 +146,7 @@ final class DecodeHandler extends Handler { if (rawResult != null) { // Don't log the barcode contents for security. long end = System.currentTimeMillis(); - Log.d(TAG, "Found barcode in " + (end - start) + " ms"); + LogUtils.d("Found barcode in " + (end - start) + " ms"); BarcodeFormat barcodeFormat = rawResult.getBarcodeFormat(); if(handler!=null && handler.isSupportAutoZoom() && barcodeFormat == BarcodeFormat.QR_CODE){ @@ -242,7 +237,7 @@ final class DecodeHandler extends Handler { lastZoomTime = System.currentTimeMillis(); return true; } else { - Log.i(TAG, "Zoom not supported"); + LogUtils.d("Zoom not supported"); } } diff --git a/lib/src/main/java/com/king/zxing/DecodeThread.java b/lib/src/main/java/com/king/zxing/DecodeThread.java index 2f2d4a9..4a4b5b8 100644 --- a/lib/src/main/java/com/king/zxing/DecodeThread.java +++ b/lib/src/main/java/com/king/zxing/DecodeThread.java @@ -21,13 +21,13 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.DecodeHintType; import com.google.zxing.ResultPointCallback; import com.king.zxing.camera.CameraManager; +import com.king.zxing.util.LogUtils; import android.content.Context; import android.content.SharedPreferences; import android.os.Handler; import android.os.Looper; import android.preference.PreferenceManager; -import android.util.Log; import java.util.Collection; import java.util.EnumMap; @@ -98,7 +98,7 @@ final class DecodeThread extends Thread { hints.put(DecodeHintType.CHARACTER_SET, characterSet); } hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback); - Log.i("DecodeThread", "Hints: " + hints); + LogUtils.i("Hints: " + hints); } Handler getHandler() { diff --git a/lib/src/main/java/com/king/zxing/InactivityTimer.java b/lib/src/main/java/com/king/zxing/InactivityTimer.java index 891cad7..3664b5c 100644 --- a/lib/src/main/java/com/king/zxing/InactivityTimer.java +++ b/lib/src/main/java/com/king/zxing/InactivityTimer.java @@ -24,7 +24,8 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.AsyncTask; import android.os.BatteryManager; -import android.util.Log; + +import com.king.zxing.util.LogUtils; import java.lang.ref.WeakReference; import java.util.concurrent.RejectedExecutionException; @@ -56,7 +57,7 @@ final class InactivityTimer { try { inactivityTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (RejectedExecutionException ree) { - Log.w(TAG, "Couldn't schedule inactivity task; ignoring"); + LogUtils.w( "Couldn't schedule inactivity task; ignoring"); } } @@ -66,13 +67,13 @@ final class InactivityTimer { activity.unregisterReceiver(powerStatusReceiver); registered = false; } else { - Log.w(TAG, "PowerStatusReceiver was never registered?"); + LogUtils.w( "PowerStatusReceiver was never registered?"); } } void onResume() { if (registered) { - Log.w(TAG, "PowerStatusReceiver was already registered?"); + LogUtils.w( "PowerStatusReceiver was already registered?"); } else { activity.registerReceiver(powerStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); registered = true; @@ -130,7 +131,7 @@ final class InactivityTimer { protected Object doInBackground(Object... objects) { try { Thread.sleep(INACTIVITY_DELAY_MS); - Log.i(TAG, "Finishing activity due to inactivity"); + LogUtils.i("Finishing activity due to inactivity"); Activity activity = weakReference.get(); if(activity!=null){ activity.finish(); diff --git a/lib/src/main/java/com/king/zxing/camera/AutoFocusManager.java b/lib/src/main/java/com/king/zxing/camera/AutoFocusManager.java index 5c9659c..5c3add8 100644 --- a/lib/src/main/java/com/king/zxing/camera/AutoFocusManager.java +++ b/lib/src/main/java/com/king/zxing/camera/AutoFocusManager.java @@ -20,7 +20,6 @@ import android.content.SharedPreferences; import android.hardware.Camera; import android.os.AsyncTask; import android.preference.PreferenceManager; -import android.util.Log; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -28,11 +27,10 @@ import java.util.Collection; import java.util.concurrent.RejectedExecutionException; import com.king.zxing.Preferences; +import com.king.zxing.util.LogUtils; final class AutoFocusManager implements Camera.AutoFocusCallback { - private static final String TAG = AutoFocusManager.class.getSimpleName(); - private static final long AUTO_FOCUS_INTERVAL_MS = 1200L; private static final Collection FOCUS_MODES_CALLING_AF; static { @@ -54,7 +52,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback { useAutoFocus = sharedPrefs.getBoolean(Preferences.KEY_AUTO_FOCUS, true) && FOCUS_MODES_CALLING_AF.contains(currentFocusMode); - Log.i(TAG, "Current focus mode '" + currentFocusMode + "'; use auto focus? " + useAutoFocus); + LogUtils.i("Current focus mode '" + currentFocusMode + "'; use auto focus? " + useAutoFocus); start(); } @@ -71,7 +69,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback { newTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); outstandingTask = newTask; } catch (RejectedExecutionException ree) { - Log.w(TAG, "Could not request auto focus", ree); + LogUtils.w("Could not request auto focus", ree); } } } @@ -85,7 +83,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback { focusing = true; } catch (RuntimeException re) { // Have heard RuntimeException reported in Android 4.0.x+; continue? - Log.w(TAG, "Unexpected exception while focusing", re); + LogUtils.w("Unexpected exception while focusing", re); // Try again later to keep cycle going autoFocusAgainLater(); } @@ -111,7 +109,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback { camera.cancelAutoFocus(); } catch (RuntimeException re) { // Have heard RuntimeException reported in Android 4.0.x+; continue? - Log.w(TAG, "Unexpected exception while cancelling focusing", re); + LogUtils.w("Unexpected exception while cancelling focusing", re); } } } diff --git a/lib/src/main/java/com/king/zxing/camera/CameraConfigurationManager.java b/lib/src/main/java/com/king/zxing/camera/CameraConfigurationManager.java index 207a6f8..d1c2497 100644 --- a/lib/src/main/java/com/king/zxing/camera/CameraConfigurationManager.java +++ b/lib/src/main/java/com/king/zxing/camera/CameraConfigurationManager.java @@ -22,7 +22,6 @@ import android.content.SharedPreferences; import android.graphics.Point; import android.hardware.Camera; import android.preference.PreferenceManager; -import android.util.Log; import android.view.Display; import android.view.Surface; import android.view.WindowManager; @@ -30,6 +29,7 @@ import android.view.WindowManager; import com.king.zxing.Preferences; import com.king.zxing.camera.open.CameraFacing; import com.king.zxing.camera.open.OpenCamera; +import com.king.zxing.util.LogUtils; /** * A class which deals with reading, parsing, and setting the camera parameters which are used to @@ -38,8 +38,6 @@ import com.king.zxing.camera.open.OpenCamera; @SuppressWarnings("deprecation") // camera APIs final class CameraConfigurationManager { - private static final String TAG = "CameraConfiguration"; - private final Context context; private int cwNeededRotation; private int cwRotationFromDisplayToCamera; @@ -83,15 +81,15 @@ final class CameraConfigurationManager { throw new IllegalArgumentException("Bad rotation: " + displayRotation); } } - Log.i(TAG, "Display at: " + cwRotationFromNaturalToDisplay); + LogUtils.i("Display at: " + cwRotationFromNaturalToDisplay); int cwRotationFromNaturalToCamera = camera.getOrientation(); - Log.i(TAG, "Camera at: " + cwRotationFromNaturalToCamera); + LogUtils.i("Camera at: " + cwRotationFromNaturalToCamera); // Still not 100% sure about this. But acts like we need to flip this: if (camera.getFacing() == CameraFacing.FRONT) { cwRotationFromNaturalToCamera = (360 - cwRotationFromNaturalToCamera) % 360; - Log.i(TAG, "Front camera overriden to: " + cwRotationFromNaturalToCamera); + LogUtils.i("Front camera overriden to: " + cwRotationFromNaturalToCamera); } /* @@ -103,30 +101,30 @@ final class CameraConfigurationManager { overrideRotationString = prefs.getString(PreferencesActivity.KEY_FORCE_CAMERA_ORIENTATION, null); } if (overrideRotationString != null && !"-".equals(overrideRotationString)) { - Log.i(TAG, "Overriding camera manually to " + overrideRotationString); + LogUtils.i("Overriding camera manually to " + overrideRotationString); cwRotationFromNaturalToCamera = Integer.parseInt(overrideRotationString); } */ cwRotationFromDisplayToCamera = (360 + cwRotationFromNaturalToCamera - cwRotationFromNaturalToDisplay) % 360; - Log.i(TAG, "Final display orientation: " + cwRotationFromDisplayToCamera); + LogUtils.i("Final display orientation: " + cwRotationFromDisplayToCamera); if (camera.getFacing() == CameraFacing.FRONT) { - Log.i(TAG, "Compensating rotation for front camera"); + LogUtils.i("Compensating rotation for front camera"); cwNeededRotation = (360 - cwRotationFromDisplayToCamera) % 360; } else { cwNeededRotation = cwRotationFromDisplayToCamera; } - Log.i(TAG, "Clockwise rotation from display to camera: " + cwNeededRotation); + LogUtils.i("Clockwise rotation from display to camera: " + cwNeededRotation); Point theScreenResolution = new Point(); display.getSize(theScreenResolution); screenResolution = theScreenResolution; - Log.i(TAG, "Screen resolution in current orientation: " + screenResolution); + LogUtils.i("Screen resolution in current orientation: " + screenResolution); cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution); - Log.i(TAG, "Camera resolution: " + cameraResolution); + LogUtils.i("Camera resolution: " + cameraResolution); bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution); - Log.i(TAG, "Best available preview size: " + bestPreviewSize); + LogUtils.i("Best available preview size: " + bestPreviewSize); boolean isScreenPortrait = screenResolution.x < screenResolution.y; boolean isPreviewSizePortrait = bestPreviewSize.x < bestPreviewSize.y; @@ -136,7 +134,7 @@ final class CameraConfigurationManager { } else { previewSizeOnScreen = new Point(bestPreviewSize.y, bestPreviewSize.x); } - Log.i(TAG, "Preview size on screen: " + previewSizeOnScreen); + LogUtils.i("Preview size on screen: " + previewSizeOnScreen); } void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) { @@ -145,14 +143,14 @@ final class CameraConfigurationManager { Camera.Parameters parameters = theCamera.getParameters(); if (parameters == null) { - Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration."); + LogUtils.w("Device error: no camera parameters are available. Proceeding without configuration."); return; } - Log.i(TAG, "Initial camera parameters: " + parameters.flatten()); + LogUtils.i("Initial camera parameters: " + parameters.flatten()); if (safeMode) { - Log.w(TAG, "In camera config safe mode -- most settings will not be honored"); + LogUtils.w("In camera config safe mode -- most settings will not be honored"); } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); @@ -199,7 +197,7 @@ final class CameraConfigurationManager { Camera.Parameters afterParameters = theCamera.getParameters(); Camera.Size afterSize = afterParameters.getPreviewSize(); if (afterSize != null && (bestPreviewSize.x != afterSize.width || bestPreviewSize.y != afterSize.height)) { - Log.w(TAG, "Camera said it supported preview size " + bestPreviewSize.x + 'x' + bestPreviewSize.y + + LogUtils.w("Camera said it supported preview size " + bestPreviewSize.x + 'x' + bestPreviewSize.y + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height); bestPreviewSize.x = afterSize.width; bestPreviewSize.y = afterSize.height; diff --git a/lib/src/main/java/com/king/zxing/camera/CameraConfigurationUtils.java b/lib/src/main/java/com/king/zxing/camera/CameraConfigurationUtils.java index 15efb04..7f9a7b1 100644 --- a/lib/src/main/java/com/king/zxing/camera/CameraConfigurationUtils.java +++ b/lib/src/main/java/com/king/zxing/camera/CameraConfigurationUtils.java @@ -20,12 +20,12 @@ import android.graphics.Point; import android.graphics.Rect; import android.hardware.Camera; import android.os.Build; -import android.util.Log; + +import com.king.zxing.util.LogUtils; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; @@ -38,8 +38,6 @@ import java.util.regex.Pattern; @SuppressWarnings("deprecation") // camera APIs public final class CameraConfigurationUtils { - private static final String TAG = "CameraConfiguration"; - private static final Pattern SEMICOLON = Pattern.compile(";"); private static final int MIN_PREVIEW_PIXELS = 480 * 320; // normal screen @@ -81,7 +79,7 @@ public final class CameraConfigurationUtils { } if (focusMode != null) { if (focusMode.equals(parameters.getFocusMode())) { - Log.i(TAG, "Focus mode already set to " + focusMode); + LogUtils.d( "Focus mode already set to " + focusMode); } else { parameters.setFocusMode(focusMode); } @@ -103,9 +101,9 @@ public final class CameraConfigurationUtils { } if (flashMode != null) { if (flashMode.equals(parameters.getFlashMode())) { - Log.i(TAG, "Flash mode already set to " + flashMode); + LogUtils.d( "Flash mode already set to " + flashMode); } else { - Log.i(TAG, "Setting flash mode to " + flashMode); + LogUtils.d( "Setting flash mode to " + flashMode); parameters.setFlashMode(flashMode); } } @@ -123,13 +121,13 @@ public final class CameraConfigurationUtils { // Clamp value: compensationSteps = Math.max(Math.min(compensationSteps, maxExposure), minExposure); if (parameters.getExposureCompensation() == compensationSteps) { - Log.i(TAG, "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation); + LogUtils.d( "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation); } else { - Log.i(TAG, "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation); + LogUtils.d( "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation); parameters.setExposureCompensation(compensationSteps); } } else { - Log.i(TAG, "Camera does not support exposure compensation"); + LogUtils.d( "Camera does not support exposure compensation"); } } @@ -139,7 +137,7 @@ public final class CameraConfigurationUtils { public static void setBestPreviewFPS(Camera.Parameters parameters, int minFPS, int maxFPS) { List supportedPreviewFpsRanges = parameters.getSupportedPreviewFpsRange(); - Log.i(TAG, "Supported FPS ranges: " + toString(supportedPreviewFpsRanges)); + LogUtils.d( "Supported FPS ranges: " + toString(supportedPreviewFpsRanges)); if (supportedPreviewFpsRanges != null && !supportedPreviewFpsRanges.isEmpty()) { int[] suitableFPSRange = null; for (int[] fpsRange : supportedPreviewFpsRanges) { @@ -151,14 +149,14 @@ public final class CameraConfigurationUtils { } } if (suitableFPSRange == null) { - Log.i(TAG, "No suitable FPS range?"); + LogUtils.d( "No suitable FPS range?"); } else { int[] currentFpsRange = new int[2]; parameters.getPreviewFpsRange(currentFpsRange); if (Arrays.equals(currentFpsRange, suitableFPSRange)) { - Log.i(TAG, "FPS range already set to " + Arrays.toString(suitableFPSRange)); + LogUtils.d( "FPS range already set to " + Arrays.toString(suitableFPSRange)); } else { - Log.i(TAG, "Setting FPS range to " + Arrays.toString(suitableFPSRange)); + LogUtils.d( "Setting FPS range to " + Arrays.toString(suitableFPSRange)); parameters.setPreviewFpsRange(suitableFPSRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], suitableFPSRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); } @@ -168,23 +166,23 @@ public final class CameraConfigurationUtils { public static void setFocusArea(Camera.Parameters parameters) { if (parameters.getMaxNumFocusAreas() > 0) { - Log.i(TAG, "Old focus areas: " + toString(parameters.getFocusAreas())); + LogUtils.d( "Old focus areas: " + toString(parameters.getFocusAreas())); List middleArea = buildMiddleArea(AREA_PER_1000); - Log.i(TAG, "Setting focus area to : " + toString(middleArea)); + LogUtils.d( "Setting focus area to : " + toString(middleArea)); parameters.setFocusAreas(middleArea); } else { - Log.i(TAG, "Device does not support focus areas"); + LogUtils.d( "Device does not support focus areas"); } } public static void setMetering(Camera.Parameters parameters) { if (parameters.getMaxNumMeteringAreas() > 0) { - Log.i(TAG, "Old metering areas: " + parameters.getMeteringAreas()); + LogUtils.d( "Old metering areas: " + parameters.getMeteringAreas()); List middleArea = buildMiddleArea(AREA_PER_1000); - Log.i(TAG, "Setting metering area to : " + toString(middleArea)); + LogUtils.d( "Setting metering area to : " + toString(middleArea)); parameters.setMeteringAreas(middleArea); } else { - Log.i(TAG, "Device does not support metering areas"); + LogUtils.d( "Device does not support metering areas"); } } @@ -196,19 +194,19 @@ public final class CameraConfigurationUtils { public static void setVideoStabilization(Camera.Parameters parameters) { if (parameters.isVideoStabilizationSupported()) { if (parameters.getVideoStabilization()) { - Log.i(TAG, "Video stabilization already enabled"); + LogUtils.d( "Video stabilization already enabled"); } else { - Log.i(TAG, "Enabling video stabilization..."); + LogUtils.d( "Enabling video stabilization..."); parameters.setVideoStabilization(true); } } else { - Log.i(TAG, "This device does not support video stabilization"); + LogUtils.d( "This device does not support video stabilization"); } } public static void setBarcodeSceneMode(Camera.Parameters parameters) { if (Camera.Parameters.SCENE_MODE_BARCODE.equals(parameters.getSceneMode())) { - Log.i(TAG, "Barcode scene mode already set"); + LogUtils.d( "Barcode scene mode already set"); return; } String sceneMode = findSettableValue("scene mode", @@ -226,22 +224,22 @@ public final class CameraConfigurationUtils { return; } if (parameters.getZoom() == zoom) { - Log.i(TAG, "Zoom is already set to " + zoom); + LogUtils.d( "Zoom is already set to " + zoom); } else { - Log.i(TAG, "Setting zoom to " + zoom); + LogUtils.d( "Setting zoom to " + zoom); parameters.setZoom(zoom); } } else { - Log.i(TAG, "Zoom is not supported"); + LogUtils.d( "Zoom is not supported"); } } private static Integer indexOfClosestZoom(Camera.Parameters parameters, double targetZoomRatio) { List ratios = parameters.getZoomRatios(); - Log.i(TAG, "Zoom ratios: " + ratios); + LogUtils.d( "Zoom ratios: " + ratios); int maxZoom = parameters.getMaxZoom(); if (ratios == null || ratios.isEmpty() || ratios.size() != maxZoom + 1) { - Log.w(TAG, "Invalid zoom ratios!"); + LogUtils.w( "Invalid zoom ratios!"); return null; } double target100 = 100.0 * targetZoomRatio; @@ -254,13 +252,13 @@ public final class CameraConfigurationUtils { closestIndex = i; } } - Log.i(TAG, "Chose zoom ratio of " + (ratios.get(closestIndex) / 100.0)); + LogUtils.d( "Chose zoom ratio of " + (ratios.get(closestIndex) / 100.0)); return closestIndex; } public static void setInvertColor(Camera.Parameters parameters) { if (Camera.Parameters.EFFECT_NEGATIVE.equals(parameters.getColorEffect())) { - Log.i(TAG, "Negative effect already set"); + LogUtils.d( "Negative effect already set"); return; } String colorMode = findSettableValue("color effect", @@ -275,7 +273,7 @@ public final class CameraConfigurationUtils { List rawSupportedSizes = parameters.getSupportedPreviewSizes(); if (rawSupportedSizes == null) { - Log.w(TAG, "Device returned no supported preview sizes; using default"); + LogUtils.w( "Device returned no supported preview sizes; using default"); Camera.Size defaultSize = parameters.getPreviewSize(); if (defaultSize == null) { throw new IllegalStateException("Parameters contained no preview size!"); @@ -284,12 +282,12 @@ public final class CameraConfigurationUtils { } - if (Log.isLoggable(TAG, Log.INFO)) { + if (LogUtils.isShowLog()) { StringBuilder previewSizesString = new StringBuilder(); for (Camera.Size size : rawSupportedSizes) { previewSizesString.append(size.width).append('x').append(size.height).append(' '); } - Log.i(TAG, "Supported preview sizes: " + previewSizesString); + LogUtils.d( "Supported preview sizes: " + previewSizesString); } double screenAspectRatio; @@ -298,7 +296,7 @@ public final class CameraConfigurationUtils { }else{ screenAspectRatio = screenResolution.y / (double) screenResolution.x; } - Log.i(TAG, "screenAspectRatio: " + screenAspectRatio); + LogUtils.d( "screenAspectRatio: " + screenAspectRatio); // Find a suitable size, with max resolution int maxResolution = 0; @@ -314,19 +312,19 @@ public final class CameraConfigurationUtils { boolean isCandidatePortrait = realWidth < realHeight; int maybeFlippedWidth = isCandidatePortrait ? realWidth: realHeight ; int maybeFlippedHeight = isCandidatePortrait ? realHeight : realWidth; - Log.i(TAG, String.format("maybeFlipped:%d * %d",maybeFlippedWidth,maybeFlippedHeight)); + LogUtils.d( String.format("maybeFlipped:%d * %d",maybeFlippedWidth,maybeFlippedHeight)); double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight; - Log.i(TAG, "aspectRatio: " + aspectRatio); + LogUtils.d( "aspectRatio: " + aspectRatio); double distortion = Math.abs(aspectRatio - screenAspectRatio); - Log.i(TAG, "distortion: " + distortion); + LogUtils.d( "distortion: " + distortion); if (distortion > MAX_ASPECT_DISTORTION) { continue; } if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) { Point exactPoint = new Point(realWidth, realHeight); - Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint); + LogUtils.d( "Found preview size exactly matching screen size: " + exactPoint); return exactPoint; } @@ -342,7 +340,7 @@ public final class CameraConfigurationUtils { // the CPU is much more powerful. if (maxResPreviewSize != null) { Point largestSize = new Point(maxResPreviewSize.width, maxResPreviewSize.height); - Log.i(TAG, "Using largest suitable preview size: " + largestSize); + LogUtils.d( "Using largest suitable preview size: " + largestSize); return largestSize; } @@ -352,24 +350,24 @@ public final class CameraConfigurationUtils { throw new IllegalStateException("Parameters contained no preview size!"); } Point defaultSize = new Point(defaultPreview.width, defaultPreview.height); - Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize); + LogUtils.d( "No suitable preview sizes, using default: " + defaultSize); return defaultSize; } private static String findSettableValue(String name, Collection supportedValues, String... desiredValues) { - Log.i(TAG, "Requesting " + name + " value from among: " + Arrays.toString(desiredValues)); - Log.i(TAG, "Supported " + name + " values: " + supportedValues); + LogUtils.d( "Requesting " + name + " value from among: " + Arrays.toString(desiredValues)); + LogUtils.d( "Supported " + name + " values: " + supportedValues); if (supportedValues != null) { for (String desiredValue : desiredValues) { if (supportedValues.contains(desiredValue)) { - Log.i(TAG, "Can set " + name + " to: " + desiredValue); + LogUtils.d( "Can set " + name + " to: " + desiredValue); return desiredValue; } } } - Log.i(TAG, "No supported values match"); + LogUtils.d( "No supported values match"); return null; } diff --git a/lib/src/main/java/com/king/zxing/camera/CameraManager.java b/lib/src/main/java/com/king/zxing/camera/CameraManager.java index 1448262..6d5ca37 100644 --- a/lib/src/main/java/com/king/zxing/camera/CameraManager.java +++ b/lib/src/main/java/com/king/zxing/camera/CameraManager.java @@ -22,12 +22,12 @@ import android.graphics.Point; import android.graphics.Rect; import android.hardware.Camera; import android.os.Handler; -import android.util.Log; import android.view.SurfaceHolder; import com.google.zxing.PlanarYUVLuminanceSource; import com.king.zxing.camera.open.OpenCamera; import com.king.zxing.camera.open.OpenCameraInterface; +import com.king.zxing.util.LogUtils; import java.io.IOException; @@ -43,8 +43,6 @@ import androidx.annotation.FloatRange; @SuppressWarnings("deprecation") // camera APIs public final class CameraManager { - private static final String TAG = CameraManager.class.getSimpleName(); - private static final int MIN_FRAME_WIDTH = 240; private static final int MIN_FRAME_HEIGHT = 240; private static final int MAX_FRAME_WIDTH = 1200; // = 5/8 * 1920 @@ -117,8 +115,8 @@ public final class CameraManager { configManager.setDesiredCameraParameters(theCamera, false); } catch (RuntimeException re) { // Driver failed - Log.w(TAG, "Camera rejected parameters. Setting only minimal safe-mode parameters"); - Log.i(TAG, "Resetting to saved camera params: " + parametersFlattened); + LogUtils.w("Camera rejected parameters. Setting only minimal safe-mode parameters"); + LogUtils.i( "Resetting to saved camera params: " + parametersFlattened); // Reset: if (parametersFlattened != null) { parameters = cameraObject.getParameters(); @@ -128,7 +126,7 @@ public final class CameraManager { configManager.setDesiredCameraParameters(theCamera, true); } catch (RuntimeException re2) { // Well, darn. Give up - Log.w(TAG, "Camera rejected even safe-mode parameters! No configuration"); + LogUtils.w("Camera rejected even safe-mode parameters! No configuration"); } } } @@ -360,7 +358,7 @@ public final class CameraManager { int leftOffset = (screenResolution.x - width) / 2; int topOffset = (screenResolution.y - height) / 2; framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height); - Log.d(TAG, "Calculated manual framing rect: " + framingRect); + LogUtils.d( "Calculated manual framing rect: " + framingRect); framingRectInPreview = null; } else { requestedFramingRectWidth = width; diff --git a/lib/src/main/java/com/king/zxing/camera/PreviewCallback.java b/lib/src/main/java/com/king/zxing/camera/PreviewCallback.java index bdc6bbd..a115110 100644 --- a/lib/src/main/java/com/king/zxing/camera/PreviewCallback.java +++ b/lib/src/main/java/com/king/zxing/camera/PreviewCallback.java @@ -21,7 +21,8 @@ import android.graphics.Point; import android.hardware.Camera; import android.os.Handler; import android.os.Message; -import android.util.Log; + +import com.king.zxing.util.LogUtils; @SuppressWarnings("deprecation") // camera APIs final class PreviewCallback implements Camera.PreviewCallback { @@ -51,7 +52,7 @@ final class PreviewCallback implements Camera.PreviewCallback { message.sendToTarget(); previewHandler = null; } else { - Log.d(TAG, "Got preview callback, but no handler or resolution available"); + LogUtils.d("Got preview callback, but no handler or resolution available"); } } diff --git a/lib/src/main/java/com/king/zxing/camera/open/OpenCameraInterface.java b/lib/src/main/java/com/king/zxing/camera/open/OpenCameraInterface.java index c82d832..dad92f9 100644 --- a/lib/src/main/java/com/king/zxing/camera/open/OpenCameraInterface.java +++ b/lib/src/main/java/com/king/zxing/camera/open/OpenCameraInterface.java @@ -18,7 +18,8 @@ package com.king.zxing.camera.open; import android.hardware.Camera; -import android.util.Log; + +import com.king.zxing.util.LogUtils; /** * Abstraction over the {@link Camera} API that helps open them and return their metadata. @@ -26,8 +27,6 @@ import android.util.Log; @SuppressWarnings("deprecation") // camera APIs public final class OpenCameraInterface { - private static final String TAG = OpenCameraInterface.class.getName(); - /** * For {@link #open(int)}, means no preference for which camera to open. */ @@ -48,11 +47,11 @@ public final class OpenCameraInterface { int numCameras = Camera.getNumberOfCameras(); if (numCameras == 0) { - Log.w(TAG, "No cameras!"); + LogUtils.w("No cameras!"); return null; } if (cameraId >= numCameras) { - Log.w(TAG, "Requested camera does not exist: " + cameraId); + LogUtils.w("Requested camera does not exist: " + cameraId); return null; } @@ -67,12 +66,12 @@ public final class OpenCameraInterface { cameraId++; } if (cameraId == numCameras) { - Log.i(TAG, "No camera facing " + CameraFacing.BACK + "; returning camera #0"); + LogUtils.i("No camera facing " + CameraFacing.BACK + "; returning camera #0"); cameraId = 0; } } - Log.i(TAG, "Opening camera #" + cameraId); + LogUtils.i("Opening camera #" + cameraId); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, cameraInfo); Camera camera = Camera.open(cameraId); diff --git a/lib/src/main/java/com/king/zxing/util/CodeUtils.java b/lib/src/main/java/com/king/zxing/util/CodeUtils.java index 8eed17d..b77a11f 100644 --- a/lib/src/main/java/com/king/zxing/util/CodeUtils.java +++ b/lib/src/main/java/com/king/zxing/util/CodeUtils.java @@ -20,9 +20,13 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; + import android.text.TextPaint; import android.text.TextUtils; -import android.util.Log; + +import androidx.annotation.ColorInt; +import androidx.annotation.FloatRange; +import androidx.annotation.NonNull; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; @@ -30,7 +34,6 @@ import com.google.zxing.DecodeHintType; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; -import com.google.zxing.NotFoundException; import com.google.zxing.RGBLuminanceSource; import com.google.zxing.Result; import com.google.zxing.WriterException; @@ -40,23 +43,21 @@ import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeReader; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; -import com.king.zxing.CaptureHelper; import com.king.zxing.DecodeFormatManager; import java.util.HashMap; import java.util.Map; import java.util.Vector; -import androidx.annotation.ColorInt; -import androidx.annotation.FloatRange; -import androidx.annotation.NonNull; - /** * @author Jenly Jenly */ public final class CodeUtils { + public static final int DEFAULT_REQ_WIDTH = 450; + public static final int DEFAULT_REQ_HEIGHT = 800; + private CodeUtils(){ throw new AssertionError(); } @@ -140,7 +141,7 @@ public final class CodeUtils { //容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //设置空白边距的宽度 - hints.put(EncodeHintType.MARGIN, 1); //default is 4 + hints.put(EncodeHintType.MARGIN, 1); //default is 1 return createQRCode(content,heightPix,logo,ratio,hints,codeColor); } @@ -186,7 +187,7 @@ public final class CodeUtils { return bitmap; } catch (WriterException e) { - Log.w(CaptureHelper.TAG,e.getMessage()); + LogUtils.w(e.getMessage()); } return null; @@ -234,7 +235,7 @@ public final class CodeUtils { canvas.restore(); } catch (Exception e) { bitmap = null; - Log.w(CaptureHelper.TAG,e.getMessage()); + LogUtils.w(e.getMessage()); } return bitmap; @@ -273,11 +274,23 @@ public final class CodeUtils { * @return */ public static Result parseQRCodeResult(String bitmapPath, Map hints){ + return parseQRCodeResult(bitmapPath,DEFAULT_REQ_WIDTH,DEFAULT_REQ_HEIGHT,hints); + } + + /** + * 解析二维码图片 + * @param bitmapPath + * @param reqWidth + * @param reqHeight + * @param hints + * @return + */ + public static Result parseQRCodeResult(String bitmapPath,int reqWidth,int reqHeight,Map hints){ Result result = null; try{ QRCodeReader reader = new QRCodeReader(); - RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath)); + RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath,reqWidth,reqHeight)); if (source != null) { boolean isReDecode; @@ -322,7 +335,7 @@ public final class CodeUtils { } }catch (Exception e){ - Log.w(CaptureHelper.TAG,e.getMessage()); + LogUtils.w(e.getMessage()); } return result; @@ -370,12 +383,24 @@ public final class CodeUtils { * @return */ public static Result parseCodeResult(String bitmapPath, Map hints){ + return parseCodeResult(bitmapPath,DEFAULT_REQ_WIDTH,DEFAULT_REQ_HEIGHT,hints); + } + + /** + * 解析一维码/二维码图片 + * @param bitmapPath + * @param reqWidth + * @param reqHeight + * @param hints 解析编码类型 + * @return + */ + public static Result parseCodeResult(String bitmapPath,int reqWidth,int reqHeight, Map hints){ Result result = null; try{ MultiFormatReader reader = new MultiFormatReader(); reader.setHints(hints); - RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath)); + RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath,reqWidth,reqHeight)); if (source != null) { boolean isReDecode; @@ -420,7 +445,7 @@ public final class CodeUtils { } }catch (Exception e){ - Log.w(CaptureHelper.TAG,e.getMessage()); + LogUtils.w(e.getMessage()); } return result; @@ -433,31 +458,33 @@ public final class CodeUtils { * @param path * @return */ - private static Bitmap compressBitmap(String path){ + private static Bitmap compressBitmap(String path,int reqWidth,int reqHeight){ BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true;//获取原始图片大小 BitmapFactory.decodeFile(path, newOpts);// 此时返回bm为空 - int w = newOpts.outWidth; - int h = newOpts.outHeight; - float width = 800f; - float height = 480f; + float width = newOpts.outWidth; + float height = newOpts.outHeight; // 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 - int be = 1;// be=1表示不缩放 - if (w > h && w > width) {// 如果宽度大的话根据宽度固定大小缩放 - be = (int) (newOpts.outWidth / width); - } else if (w < h && h > height) {// 如果高度高的话根据宽度固定大小缩放 - be = (int) (newOpts.outHeight / height); + int wSize = 1;// wSize=1表示不缩放 + if (width > reqWidth) {// 如果宽度大的话根据宽度固定大小缩放 + wSize = (int) (width / reqWidth); } - if (be <= 0) - be = 1; - newOpts.inSampleSize = be;// 设置缩放比例 + int hSize = 1;// wSize=1表示不缩放 + if (height > reqHeight) {// 如果高度高的话根据宽度固定大小缩放 + hSize = (int) (height / reqHeight); + } + int size = Math.max(wSize,hSize); + if (size <= 0) + size = 1; + newOpts.inSampleSize = size;// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 newOpts.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, newOpts); } + /** * 获取RGBLuminanceSource * @param bitmap @@ -610,7 +637,7 @@ public final class CodeUtils { } return bitmap; } catch (WriterException e) { - Log.w(CaptureHelper.TAG,e.getMessage()); + LogUtils.w(e.getMessage()); } return null; } @@ -623,7 +650,7 @@ public final class CodeUtils { * @param textColor * @return */ - private static Bitmap addCode(Bitmap src,String code,int textSize,@ColorInt int textColor,int offset) { + private static Bitmap addCode(Bitmap src, String code, int textSize, @ColorInt int textColor, int offset) { if (src == null) { return null; } @@ -653,7 +680,7 @@ public final class CodeUtils { canvas.restore(); } catch (Exception e) { bitmap = null; - Log.w(CaptureHelper.TAG,e.getMessage()); + LogUtils.w(e.getMessage()); } return bitmap; diff --git a/lib/src/main/java/com/king/zxing/util/LogUtils.java b/lib/src/main/java/com/king/zxing/util/LogUtils.java new file mode 100644 index 0000000..ce5f59f --- /dev/null +++ b/lib/src/main/java/com/king/zxing/util/LogUtils.java @@ -0,0 +1,316 @@ +/* + Copyright © 2015, 2016 Jenly Yu Jenly + + 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.util; + + +import android.util.Log; + +/** + * @author Jenly Jenly + */ +public class LogUtils { + + public static final String TAG = "ZXingLite"; + + public static final String COLON = ":"; + + public static final String VERTICAL = "|"; + + /** 是否显示Log日志 */ + private static boolean isShowLog = true; + + /** Log日志优先权 */ + private static int priority = 1; + + /** + * Priority constant for the println method;use System.out.println + */ + public static final int PRINTLN = 1; + + /** + * Priority constant for the println method; use Log.v. + */ + public static final int VERBOSE = 2; + + /** + * Priority constant for the println method; use Log.d. + */ + public static final int DEBUG = 3; + + /** + * Priority constant for the println method; use Log.i. + */ + public static final int INFO = 4; + + /** + * Priority constant for the println method; use Log.w. + */ + public static final int WARN = 5; + + /** + * Priority constant for the println method; use Log.e. + */ + public static final int ERROR = 6; + + /** + * Priority constant for the println method.use Log.wtf. + */ + public static final int ASSERT = 7; + + public static final String TAG_FORMAT = "%s.%s(L:%d)"; + + private LogUtils(){ + throw new AssertionError(); + } + + public static void setShowLog(boolean isShowLog) { + + LogUtils.isShowLog = isShowLog; + } + + public static boolean isShowLog() { + + return isShowLog; + } + + public static int getPriority() { + + return priority; + } + + public static void setPriority(int priority) { + + LogUtils.priority = priority; + } + + /** + * 根据堆栈生成TAG + * @return TAG|className.methodName(L:lineNumber) + */ + private static String generateTag(StackTraceElement caller) { + String tag = TAG_FORMAT; + String callerClazzName = caller.getClassName(); + callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1); + tag = String.format(tag,new Object[] { callerClazzName, caller.getMethodName(),Integer.valueOf(caller.getLineNumber()) }); + return new StringBuilder().append(TAG).append(VERTICAL).append(tag).toString(); + } + + /** + * 获取堆栈 + * @param n + * n=0 VMStack + * n=1 Thread + * n=3 CurrentStack + * n=4 CallerStack + * ... + * @return + */ + public static StackTraceElement getStackTraceElement(int n) { + return Thread.currentThread().getStackTrace()[n]; + } + + /** + * 获取调用方的堆栈TAG + * @return + */ + private static String getCallerStackLogTag(){ + return generateTag(getStackTraceElement(5)); + } + + /** + * + * @param t + * @return + */ + private static String getStackTraceString(Throwable t){ + return Log.getStackTraceString(t); + } + + // -----------------------------------Log.v + + /** + * Log.v + * @param msg + */ + public static void v(String msg) { + if (isShowLog && priority <= VERBOSE) + Log.v(getCallerStackLogTag(), String.valueOf(msg)); + + } + + public static void v(Throwable t) { + if (isShowLog && priority <= VERBOSE) + Log.v(getCallerStackLogTag(), getStackTraceString(t)); + } + + public static void v(String msg,Throwable t) { + if (isShowLog && priority <= VERBOSE) + Log.v(getCallerStackLogTag(), String.valueOf(msg), t); + } + + // -----------------------------------Log.d + + /** + * Log.d + * @param msg + */ + public static void d(String msg) { + if (isShowLog && priority <= DEBUG) + Log.d(getCallerStackLogTag(), String.valueOf(msg)); + } + + public static void d(Throwable t) { + if (isShowLog && priority <= DEBUG) + Log.d(getCallerStackLogTag(), getStackTraceString(t)); + } + + public static void d(String msg,Throwable t) { + if (isShowLog && priority <= DEBUG) + Log.d(getCallerStackLogTag(), String.valueOf(msg), t); + } + + // -----------------------------------Log.i + + /** + * Log.i + * @param msg + */ + public static void i(String msg) { + if (isShowLog && priority <= INFO) + Log.i(getCallerStackLogTag(), String.valueOf(msg)); + } + + public static void i(Throwable t) { + if (isShowLog && priority <= INFO) + Log.i(getCallerStackLogTag(), getStackTraceString(t)); + } + + public static void i(String msg,Throwable t) { + if (isShowLog && priority <= INFO) + Log.i(getCallerStackLogTag(), String.valueOf(msg), t); + } + + // -----------------------------------Log.w + + /** + * Log.w + * @param msg + */ + public static void w(String msg) { + if (isShowLog && priority <= WARN) + Log.w(getCallerStackLogTag(), String.valueOf(msg)); + } + + public static void w(Throwable t) { + if (isShowLog && priority <= WARN) + Log.w(getCallerStackLogTag(), getStackTraceString(t)); + } + + public static void w(String msg,Throwable t) { + if (isShowLog && priority <= WARN) + Log.w(getCallerStackLogTag(), String.valueOf(msg), t); + } + + // -----------------------------------Log.e + + /** + * Log.e + * @param msg + */ + public static void e(String msg) { + if (isShowLog && priority <= ERROR) + Log.e(getCallerStackLogTag(), String.valueOf(msg)); + } + + public static void e(Throwable t) { + if (isShowLog && priority <= ERROR) + Log.e(getCallerStackLogTag(), getStackTraceString(t)); + } + + public static void e(String msg,Throwable t) { + if (isShowLog && priority <= ERROR) + Log.e(getCallerStackLogTag(), String.valueOf(msg), t); + } + + // -----------------------------------Log.wtf + + /** + * Log.wtf + * @param msg + */ + public static void wtf(String msg) { + if (isShowLog && priority <= ASSERT) + Log.wtf(getCallerStackLogTag(), String.valueOf(msg)); + } + + public static void wtf(Throwable t) { + if (isShowLog && priority <= ASSERT) + Log.wtf(getCallerStackLogTag(), getStackTraceString(t)); + } + + public static void wtf(String msg,Throwable t) { + if (isShowLog && priority <= ASSERT) + Log.wtf(getCallerStackLogTag(), String.valueOf(msg), t); + } + + // -----------------------------------System.out.print + + /** + * System.out.print + * + * @param msg + */ + public static void print(String msg) { + if (isShowLog && priority <= PRINTLN) + System.out.print(msg); + } + + public static void print(Object obj) { + if (isShowLog && priority <= PRINTLN) + System.out.print(obj); + } + + // -----------------------------------System.out.printf + + /** + * System.out.printf + * + * @param msg + */ + public static void printf(String msg) { + if (isShowLog && priority <= PRINTLN) + System.out.printf(msg); + } + + // -----------------------------------System.out.println + + /** + * System.out.println + * + * @param msg + */ + public static void println(String msg) { + if (isShowLog && priority <= PRINTLN) + System.out.println(msg); + } + + public static void println(Object obj) { + if (isShowLog && priority <= PRINTLN) + System.out.println(obj); + } + +} diff --git a/versions.gradle b/versions.gradle index 33d8866..9fb4097 100644 --- a/versions.gradle +++ b/versions.gradle @@ -1,7 +1,7 @@ //App def app_version = [:] -app_version.versionCode = 22 -app_version.versionName = "1.1.7-androidx" +app_version.versionCode = 24 +app_version.versionName = "1.1.8-androidx" ext.app_version = app_version //build version