diff --git a/preview/zxingscanner-1.2.1.jar b/preview/zxingscanner-1.2.1.jar new file mode 100644 index 0000000..434ba2b Binary files /dev/null and b/preview/zxingscanner-1.2.1.jar differ diff --git a/zxingscanner/build.gradle b/zxingscanner/build.gradle index 982ed0f..255a785 100644 --- a/zxingscanner/build.gradle +++ b/zxingscanner/build.gradle @@ -33,16 +33,14 @@ install { project { packaging 'aar' artifactId 'zxingscanner' - name 'android zxing style' //添加项目描述 + name 'android zxing style' url siteUrl - //设置开源证书信息 licenses { license { name 'The Apache Software License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } - //填写开发者基本信息 developers { developer { id 'mylhyl' @@ -80,22 +78,19 @@ artifacts { archives javadocJar archives sourcesJar } -//配置上传bintray相关信息 Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) bintray { - //读取bintray帐号和密码。 - //一般的为了保密和安全性,在项目的local.properties文件中添加两行句话即可: user = properties.getProperty("BINTRAY_USER") key = properties.getProperty("BINTRAY_KEY") configurations = ['archives'] pkg { - repo = "maven" //上传的中央仓库名称 - name = "zxingscanner" //上传的项目的名字 + repo = "maven" + name = "zxingscanner" websiteUrl = siteUrl vcsUrl = gitUrl licenses = ["Apache-2.0"] - publish = true //是否发布 + publish = true } } diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerView.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerView.java index fbe2bfe..8e9009c 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerView.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerView.java @@ -12,7 +12,6 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.Result; import com.google.zxing.ResultPoint; -import com.google.zxing.client.result.ParsedResult; import com.mylhyl.zxing.scanner.camera.CameraManager; import com.mylhyl.zxing.scanner.common.Scanner; @@ -55,10 +54,12 @@ private void init(Context context, AttributeSet attrs, int defStyle) { mBeepManager = new BeepManager(context); mSurfaceView = new SurfaceView(context, attrs, defStyle); - addView(mSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + addView(mSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT + , LayoutParams.MATCH_PARENT)); mViewfinderView = new ViewfinderView(context, attrs); - addView(mViewfinderView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + addView(mViewfinderView, new LayoutParams(LayoutParams.MATCH_PARENT + , LayoutParams.MATCH_PARENT)); } public void onResume() { @@ -132,8 +133,8 @@ public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) { //扫描成功 if (mScannerCompletionListener != null) { //转换结果 - ParsedResult parsedResult = Scanner.parseResult(rawResult); - mScannerCompletionListener.OnScannerCompletion(rawResult, parsedResult, barcode); + mScannerCompletionListener.OnScannerCompletion(rawResult, + Scanner.parseResult(rawResult), barcode); } //设置扫描结果图片 if (barcode != null) { @@ -291,7 +292,8 @@ public void setLaserFrameCornerWidth(int laserFrameCornerWidth) { * @param isBottom 是否在扫描框下方 * @param textMargin 离扫描框间距 dp */ - public void setDrawText(String text, int textSize, int textColor, boolean isBottom, int textMargin) { + public void setDrawText(String text, int textSize, int textColor + , boolean isBottom, int textMargin) { mViewfinderView.setDrawText(text, textSize, textColor, isBottom, textMargin); } diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerViewHandler.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerViewHandler.java index 4a6c016..dd96241 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerViewHandler.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ScannerViewHandler.java @@ -26,6 +26,7 @@ import com.google.zxing.DecodeHintType; import com.google.zxing.Result; import com.mylhyl.zxing.scanner.camera.CameraManager; +import com.mylhyl.zxing.scanner.common.Intents; import com.mylhyl.zxing.scanner.common.Scanner; import com.mylhyl.zxing.scanner.decode.DecodeThread; @@ -87,16 +88,13 @@ public void handleMessage(Message message) { 0, compressedBitmap.length, null); barcode = barcode.copy(Bitmap.Config.ARGB_8888, true); } - scaleFactor = bundle - .getFloat(DecodeThread.BARCODE_SCALED_FACTOR); + scaleFactor = bundle.getFloat(DecodeThread.BARCODE_SCALED_FACTOR); } - scannerView - .handleDecode((Result) message.obj, barcode, scaleFactor); + scannerView.handleDecode((Result) message.obj, barcode, scaleFactor); break; case Scanner.DECODE_FAILED: state = State.PREVIEW; - cameraManager.requestPreviewFrame(decodeThread.getHandler(), - Scanner.DECODE); + cameraManager.requestPreviewFrame(decodeThread.getHandler(), Scanner.DECODE); break; case Scanner.RETURN_SCAN_RESULT: break; @@ -123,8 +121,7 @@ public void quitSynchronously() { private void restartPreviewAndDecode() { if (state == State.SUCCESS) { state = State.PREVIEW; - cameraManager.requestPreviewFrame(decodeThread.getHandler(), - Scanner.DECODE); + cameraManager.requestPreviewFrame(decodeThread.getHandler(), Scanner.DECODE); scannerView.drawViewfinder(); } } diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ViewfinderView.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ViewfinderView.java index 755fcfc..db525fb 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ViewfinderView.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/ViewfinderView.java @@ -42,6 +42,7 @@ * @author dswitkin@google.com (Daniel Switkin) */ final class ViewfinderView extends View { + private static final int CURRENT_POINT_OPACITY = 0xA0; private static final int MAX_RESULT_POINTS = 20; private static final int POINT_SIZE = 6; @@ -182,8 +183,8 @@ private void drawFrameCorner(Canvas canvas, Rect frame) { // 左上角 canvas.drawRect(frame.left - laserFrameCornerWidth, frame.top, frame.left, frame.top + laserFrameCornerLength, paint); - canvas.drawRect(frame.left - laserFrameCornerWidth, frame.top - laserFrameCornerWidth, frame.left - + laserFrameCornerLength, frame.top, paint); + canvas.drawRect(frame.left - laserFrameCornerWidth, frame.top - laserFrameCornerWidth + , frame.left + laserFrameCornerLength, frame.top, paint); // 右上角 canvas.drawRect(frame.right, frame.top, frame.right + laserFrameCornerWidth, frame.top + laserFrameCornerLength, paint); @@ -224,7 +225,8 @@ private void drawLaserLine(Canvas canvas, Rect frame) { if (laserLineResId == 0) { paint.setStyle(Paint.Style.FILL); paint.setColor(laserColor);// 设置扫描线颜色 - canvas.drawRect(frame.left, laserLineTop, frame.right, laserLineTop + laserLineHeight, paint); + canvas.drawRect(frame.left, laserLineTop, frame.right + , laserLineTop + laserLineHeight, paint); } else { if (laserLineBitmap == null)//图片资源文件转为 Bitmap laserLineBitmap = BitmapFactory.decodeResource(getResources(), laserLineResId); @@ -232,7 +234,8 @@ private void drawLaserLine(Canvas canvas, Rect frame) { //网格图片 if (isLaserGridLine) { RectF dstRectF = new RectF(frame.left, frame.top, frame.right, laserLineTop); - Rect srcRect = new Rect(0, (int) (height - dstRectF.height()), laserLineBitmap.getWidth(), height); + Rect srcRect = new Rect(0, (int) (height - dstRectF.height()) + , laserLineBitmap.getWidth(), height); canvas.drawBitmap(laserLineBitmap, srcRect, dstRectF, paint); } //线条图片 @@ -241,7 +244,8 @@ private void drawLaserLine(Canvas canvas, Rect frame) { if (laserLineHeight == Scanner.dp2px(getContext(), DEFAULT_LASER_LINE_HEIGHT)) { laserLineHeight = laserLineBitmap.getHeight() / 2; } - Rect laserRect = new Rect(frame.left, laserLineTop, frame.right, laserLineTop + laserLineHeight); + Rect laserRect = new Rect(frame.left, laserLineTop, frame.right + , laserLineTop + laserLineHeight); canvas.drawBitmap(laserLineBitmap, null, laserRect, paint); } } @@ -345,7 +349,8 @@ public void setLaserFrameCornerWidth(int laserFrameCornerWidth) { this.laserFrameCornerWidth = Scanner.dp2px(getContext(), laserFrameCornerWidth); } - public void setDrawText(String text, int textSize, int textColor, boolean isBottom, int textMargin) { + public void setDrawText(String text, int textSize, int textColor + , boolean isBottom, int textMargin) { if (!TextUtils.isEmpty(text)) drawText = text; if (textSize > 0) diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/CameraConfigurationManager.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/CameraConfigurationManager.java index 2fff974..cc9be78 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/CameraConfigurationManager.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/CameraConfigurationManager.java @@ -19,6 +19,7 @@ import android.content.Context; import android.graphics.Point; import android.hardware.Camera; +import android.util.Log; import android.view.Display; import android.view.Surface; import android.view.WindowManager; @@ -34,8 +35,9 @@ */ final class CameraConfigurationManager { -// private static final String TAG = "CameraConfiguration"; - + private static final String TAG = "CameraConfiguration"; + private static final int FRONT_LIGHT_MODE_ON = 0; + private static final int FRONT_LIGHT_MODE_OFF = 1; private final Context context; private int cwNeededRotation; private int cwRotationFromDisplayToCamera; @@ -82,52 +84,29 @@ void initFromCameraParameters(OpenCamera camera) { + displayRotation); } } -// Log.i(TAG, "Display at: " + cwRotationFromNaturalToDisplay); int cwRotationFromNaturalToCamera = camera.getOrientation(); -// Log.i(TAG, "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); } - /* - * SharedPreferences prefs = - * PreferenceManager.getDefaultSharedPreferences(context); String - * overrideRotationString; if (camera.getFacing() == CameraFacing.FRONT) - * { overrideRotationString = - * prefs.getString(PreferencesActivity.KEY_FORCE_CAMERA_ORIENTATION_FRONT - * , null); } else { overrideRotationString = - * prefs.getString(PreferencesActivity.KEY_FORCE_CAMERA_ORIENTATION, - * null); } if (overrideRotationString != null && - * !"-".equals(overrideRotationString)) { Log.i(TAG, - * "Overriding camera manually to " + overrideRotationString); - * cwRotationFromNaturalToCamera = - * Integer.parseInt(overrideRotationString); } - */ - - cwRotationFromDisplayToCamera = (360 + cwRotationFromNaturalToCamera - cwRotationFromNaturalToDisplay) % 360; -// Log.i(TAG, "Final display orientation: " + cwRotationFromDisplayToCamera); + cwRotationFromDisplayToCamera = (360 + + cwRotationFromNaturalToCamera - cwRotationFromNaturalToDisplay) % 360; + if (camera.getFacing() == CameraFacing.FRONT) { -// Log.i(TAG, "Compensating rotation for front camera"); cwNeededRotation = (360 - cwRotationFromDisplayToCamera) % 360; } else { cwNeededRotation = cwRotationFromDisplayToCamera; } -// Log.i(TAG, "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); cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue( parameters, screenResolution); -// Log.i(TAG, "Camera resolution: " + cameraResolution); bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue( parameters, screenResolution); -// Log.i(TAG, "Best available preview size: " + bestPreviewSize); boolean isScreenPortrait = screenResolution.x < screenResolution.y; boolean isPreviewSizePortrait = bestPreviewSize.x < bestPreviewSize.y; @@ -135,10 +114,8 @@ void initFromCameraParameters(OpenCamera camera) { if (isScreenPortrait == isPreviewSizePortrait) { previewSizeOnScreen = bestPreviewSize; } else { - previewSizeOnScreen = new Point(bestPreviewSize.y, - bestPreviewSize.x); + previewSizeOnScreen = new Point(bestPreviewSize.y, bestPreviewSize.x); } -// Log.i(TAG, "Preview size on screen: " + previewSizeOnScreen); } /** @@ -153,18 +130,16 @@ void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) { Camera.Parameters parameters = theCamera.getParameters(); if (parameters == null) { -// Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration."); + Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration."); return; } -// Log.i(TAG, "Initial camera parameters: " + parameters.flatten()); - -// if (safeMode) { -// Log.w(TAG, "In camera config safe mode -- most settings will not be honored"); -// } + if (safeMode) { + Log.w(TAG, "In camera config safe mode -- most settings will not be honored"); + } // 默认关闪光灯 - initializeTorch(parameters, FrontLightMode.OFF, safeMode); + initializeTorch(parameters, FRONT_LIGHT_MODE_OFF, safeMode); // 自动对焦 boolean autoFocus = true; // 持续对焦 @@ -202,9 +177,8 @@ void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) { 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 -// + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height); + && (bestPreviewSize.x != afterSize.width + || bestPreviewSize.y != afterSize.height)) { bestPreviewSize.x = afterSize.width; bestPreviewSize.y = afterSize.height; } @@ -236,7 +210,8 @@ boolean getTorchState(Camera camera) { if (parameters != null) { String flashMode = camera.getParameters().getFlashMode(); return flashMode != null - && (Camera.Parameters.FLASH_MODE_ON.equals(flashMode) || Camera.Parameters.FLASH_MODE_TORCH + && (Camera.Parameters.FLASH_MODE_ON.equals(flashMode) + || Camera.Parameters.FLASH_MODE_TORCH .equals(flashMode)); } } @@ -250,8 +225,8 @@ void setTorch(Camera camera, boolean newSetting) { } private void initializeTorch(Camera.Parameters parameters, - FrontLightMode frontLightMode, boolean safeMode) { - boolean currentSetting = frontLightMode == FrontLightMode.ON; + int frontLightMode, boolean safeMode) { + boolean currentSetting = frontLightMode == FRONT_LIGHT_MODE_ON; doSetTorch(parameters, currentSetting, safeMode); } diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/FrontLightMode.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/FrontLightMode.java deleted file mode 100644 index a04f10f..0000000 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/camera/FrontLightMode.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2012 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.mylhyl.zxing.scanner.camera; - - -/** - * Enumerates settings of the preference controlling the front light. - */ -public enum FrontLightMode { - /** Always on. */ - ON, - /** Always off. */ - OFF -} diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Contents.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Contents.java deleted file mode 100644 index 8f868f7..0000000 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Contents.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.mylhyl.zxing.scanner.common; - -import android.provider.ContactsContract; - -/** - * The set of constants to use when sending Barcode Scanner an Intent which requests a barcode - * to be encoded. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public final class Contents { - private Contents() { - } - - public static final String URL_KEY = "URL_KEY"; - public static final String NOTE_KEY = "NOTE_KEY"; - /** - * When using Type.CONTACT, these arrays provide the keys for adding or retrieving multiple - * phone numbers and addresses. - */ - public static final String[] PHONE_KEYS = { - ContactsContract.Intents.Insert.PHONE, - ContactsContract.Intents.Insert.SECONDARY_PHONE, - ContactsContract.Intents.Insert.TERTIARY_PHONE - }; - - public static final String[] PHONE_TYPE_KEYS = { - ContactsContract.Intents.Insert.PHONE_TYPE, - ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, - ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE - }; - - public static final String[] EMAIL_KEYS = { - ContactsContract.Intents.Insert.EMAIL, - ContactsContract.Intents.Insert.SECONDARY_EMAIL, - ContactsContract.Intents.Insert.TERTIARY_EMAIL - }; -} diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Scanner.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Scanner.java index ff55f13..71efc8c 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Scanner.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/common/Scanner.java @@ -33,10 +33,12 @@ public static ParsedResult parseResult(Result rawResult) { } public static int dp2px(Context context, float dpValue) { - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics()); + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue + , context.getResources().getDisplayMetrics()); } public static int sp2px(Context context, float spValue) { - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics()); + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue + , context.getResources().getDisplayMetrics()); } } diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeFormatManager.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeFormatManager.java deleted file mode 100644 index 3df20be..0000000 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeFormatManager.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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.mylhyl.zxing.scanner.decode; - -import android.content.Intent; -import android.net.Uri; - -import com.google.zxing.BarcodeFormat; -import com.mylhyl.zxing.scanner.common.Intents; - -import java.util.Arrays; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.regex.Pattern; - -final class DecodeFormatManager { - - private static final Pattern COMMA_PATTERN = Pattern.compile(","); - - static final Set PRODUCT_FORMATS; - static final Set INDUSTRIAL_FORMATS; - private static final Set ONE_D_FORMATS; - static final Set QR_CODE_FORMATS = EnumSet.of(BarcodeFormat.QR_CODE); - static final Set DATA_MATRIX_FORMATS = EnumSet.of(BarcodeFormat.DATA_MATRIX); - static final Set AZTEC_FORMATS = EnumSet.of(BarcodeFormat.AZTEC); - static final Set PDF417_FORMATS = EnumSet.of(BarcodeFormat.PDF_417); - static { - PRODUCT_FORMATS = EnumSet.of(BarcodeFormat.UPC_A, - BarcodeFormat.UPC_E, - BarcodeFormat.EAN_13, - BarcodeFormat.EAN_8, - BarcodeFormat.RSS_14, - BarcodeFormat.RSS_EXPANDED); - INDUSTRIAL_FORMATS = EnumSet.of(BarcodeFormat.CODE_39, - BarcodeFormat.CODE_93, - BarcodeFormat.CODE_128, - BarcodeFormat.ITF, - BarcodeFormat.CODABAR); - ONE_D_FORMATS = EnumSet.copyOf(PRODUCT_FORMATS); - ONE_D_FORMATS.addAll(INDUSTRIAL_FORMATS); - } - private static final Map> FORMATS_FOR_MODE; - static { - FORMATS_FOR_MODE = new HashMap<>(); - FORMATS_FOR_MODE.put(Intents.Scan.ONE_D_MODE, ONE_D_FORMATS); - FORMATS_FOR_MODE.put(Intents.Scan.PRODUCT_MODE, PRODUCT_FORMATS); - FORMATS_FOR_MODE.put(Intents.Scan.QR_CODE_MODE, QR_CODE_FORMATS); - FORMATS_FOR_MODE.put(Intents.Scan.DATA_MATRIX_MODE, DATA_MATRIX_FORMATS); - FORMATS_FOR_MODE.put(Intents.Scan.AZTEC_MODE, AZTEC_FORMATS); - FORMATS_FOR_MODE.put(Intents.Scan.PDF417_MODE, PDF417_FORMATS); - } - - private DecodeFormatManager() {} - -/* static Set parseDecodeFormats(Intent intent) { - Iterable scanFormats = null; - CharSequence scanFormatsString = intent.getStringExtra(Intents.Scan.FORMATS); - if (scanFormatsString != null) { - scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString)); - } - return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE)); - }*/ - -/* static Set parseDecodeFormats(Uri inputUri) { - List formats = inputUri.getQueryParameters(Intents.Scan.FORMATS); - if (formats != null && formats.size() == 1 && formats.get(0) != null) { - formats = Arrays.asList(COMMA_PATTERN.split(formats.get(0))); - } - return parseDecodeFormats(formats, inputUri.getQueryParameter(Intents.Scan.MODE)); - }*/ - -/* private static Set parseDecodeFormats(Iterable scanFormats, String decodeMode) { - if (scanFormats != null) { - Set formats = EnumSet.noneOf(BarcodeFormat.class); - try { - for (String format : scanFormats) { - formats.add(BarcodeFormat.valueOf(format)); - } - return formats; - } catch (IllegalArgumentException iae) { - // ignore it then - } - } - if (decodeMode != null) { - return FORMATS_FOR_MODE.get(decodeMode); - } - return null; - }*/ - -} diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeHandler.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeHandler.java index 7a04d72..33ee6db 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeHandler.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeHandler.java @@ -106,8 +106,7 @@ private void decode(byte[] data, int width, int height) { if (rawResult != null) { if (handler != null) { //会向 ScannerViewHandler 发消息 - Message message = Message.obtain(handler, - Scanner.DECODE_SUCCEEDED, rawResult); + Message message = Message.obtain(handler, Scanner.DECODE_SUCCEEDED, rawResult); Bundle bundle = new Bundle(); bundleThumbnail(source, bundle); message.setData(bundle); diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeThread.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeThread.java index 626f7be..45cc68e 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeThread.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/DecodeThread.java @@ -29,6 +29,7 @@ import java.util.EnumMap; import java.util.EnumSet; import java.util.Map; +import java.util.Set; import java.util.concurrent.CountDownLatch; /** @@ -47,10 +48,30 @@ public final class DecodeThread extends Thread { private Handler handler; private final CountDownLatch handlerInitLatch; + static final Set PRODUCT_FORMATS; + static final Set INDUSTRIAL_FORMATS; + + static final Set QR_CODE_FORMATS = EnumSet.of(BarcodeFormat.QR_CODE); + static final Set DATA_MATRIX_FORMATS = EnumSet.of(BarcodeFormat.DATA_MATRIX); + + static { + PRODUCT_FORMATS = EnumSet.of(BarcodeFormat.UPC_A, + BarcodeFormat.UPC_E, + BarcodeFormat.EAN_13, + BarcodeFormat.EAN_8, + BarcodeFormat.RSS_14, + BarcodeFormat.RSS_EXPANDED); + INDUSTRIAL_FORMATS = EnumSet.of(BarcodeFormat.CODE_39, + BarcodeFormat.CODE_93, + BarcodeFormat.CODE_128, + BarcodeFormat.ITF, + BarcodeFormat.CODABAR); + } + public DecodeThread(CameraManager cameraManager, Handler scannerViewHandler, - Collection decodeFormats, - Map baseHints, String characterSet, - ResultPointCallback resultPointCallback) { + Collection decodeFormats, + Map baseHints, String characterSet, + ResultPointCallback resultPointCallback) { this.cameraManager = cameraManager; this.scannerViewHandler = scannerViewHandler; handlerInitLatch = new CountDownLatch(1); @@ -68,32 +89,22 @@ public DecodeThread(CameraManager cameraManager, Handler scannerViewHandler, // 一维码:商品 boolean decode1DProduct = true; if (decode1DProduct) { - decodeFormats.addAll(DecodeFormatManager.PRODUCT_FORMATS); + decodeFormats.addAll(PRODUCT_FORMATS); } // 一维码:工业 boolean decode1DIndustrial = true; if (decode1DIndustrial) { - decodeFormats.addAll(DecodeFormatManager.INDUSTRIAL_FORMATS); + decodeFormats.addAll(INDUSTRIAL_FORMATS); } // 二维码 boolean decodeQR = true; if (decodeQR) { - decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); + decodeFormats.addAll(QR_CODE_FORMATS); } // Data Matrix boolean decodeDataMatrix = true; if (decodeDataMatrix) { - decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS); - } - // Aztec - boolean decodeAztec = false; - if (decodeAztec) { - decodeFormats.addAll(DecodeFormatManager.AZTEC_FORMATS); - } - // PDF417 (测试) - boolean decodePDF417 = false; - if (decodePDF417) { - decodeFormats.addAll(DecodeFormatManager.PDF417_FORMATS); + decodeFormats.addAll(DATA_MATRIX_FORMATS); } } hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/QRDecode.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/QRDecode.java index 856ab16..f575894 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/QRDecode.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/decode/QRDecode.java @@ -69,8 +69,10 @@ public static void decodeQR(Bitmap srcBitmap, final OnScannerCompletionListener int height = srcBitmap.getHeight(); int[] pixels = new int[width * height]; srcBitmap.getPixels(pixels, 0, width, 0, 0, width, height); - RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);//新建一个RGBLuminanceSource对象 - BinaryBitmap binaryBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));//将图片转换成二进制图片 + //新建一个RGBLuminanceSource对象 + RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); + //将图片转换成二进制图片 + BinaryBitmap binaryBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); QRCodeReader reader = new QRCodeReader();//初始化解析对象 try { result = reader.decode(binaryBitmap, HINTS);//开始解析 @@ -82,9 +84,9 @@ public static void decodeQR(Bitmap srcBitmap, final OnScannerCompletionListener e.printStackTrace(); } } - if (listener != null) + if (listener != null) { listener.OnScannerCompletion(result, Scanner.parseResult(result), srcBitmap); - + } } private static Bitmap loadBitmap(String picturePath) throws FileNotFoundException { diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/ParserUriToVCard.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/ParserUriToVCard.java index 5cad472..5f45526 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/ParserUriToVCard.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/ParserUriToVCard.java @@ -8,12 +8,32 @@ import android.provider.BaseColumns; import android.provider.ContactsContract; -import com.mylhyl.zxing.scanner.common.Contents; - /** * Created by hupei on 2016/8/25. */ public class ParserUriToVCard { + + public static final String URL_KEY = "URL_KEY"; + public static final String NOTE_KEY = "NOTE_KEY"; + + public static final String[] PHONE_KEYS = { + ContactsContract.Intents.Insert.PHONE, + ContactsContract.Intents.Insert.SECONDARY_PHONE, + ContactsContract.Intents.Insert.TERTIARY_PHONE + }; + + public static final String[] PHONE_TYPE_KEYS = { + ContactsContract.Intents.Insert.PHONE_TYPE, + ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, + ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE + }; + + public static final String[] EMAIL_KEYS = { + ContactsContract.Intents.Insert.EMAIL, + ContactsContract.Intents.Insert.SECONDARY_EMAIL, + ContactsContract.Intents.Insert.TERTIARY_EMAIL + }; + public ParserUriToVCard() { } @@ -58,13 +78,13 @@ public Bundle parserUri(Context context, Uri contactUri) { int foundPhone = 0; int phonesNumberColumn = phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int phoneTypeColumn = phonesCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); - while (phonesCursor.moveToNext() && foundPhone < Contents.PHONE_KEYS.length) { + while (phonesCursor.moveToNext() && foundPhone < PHONE_KEYS.length) { String number = phonesCursor.getString(phonesNumberColumn); if (number != null && !number.isEmpty()) { - bundle.putString(Contents.PHONE_KEYS[foundPhone], massageContactData(number)); + bundle.putString(PHONE_KEYS[foundPhone], massageContactData(number)); } int type = phonesCursor.getInt(phoneTypeColumn); - bundle.putInt(Contents.PHONE_TYPE_KEYS[foundPhone], type); + bundle.putInt(PHONE_TYPE_KEYS[foundPhone], type); foundPhone++; } } finally { @@ -101,10 +121,10 @@ public Bundle parserUri(Context context, Uri contactUri) { try { int foundEmail = 0; int emailColumn = emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); - while (emailCursor.moveToNext() && foundEmail < Contents.EMAIL_KEYS.length) { + while (emailCursor.moveToNext() && foundEmail < EMAIL_KEYS.length) { String email = emailCursor.getString(emailColumn); if (email != null && !email.isEmpty()) { - bundle.putString(Contents.EMAIL_KEYS[foundEmail], massageContactData(email)); + bundle.putString(EMAIL_KEYS[foundEmail], massageContactData(email)); } foundEmail++; } diff --git a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/QRCodeEncoder.java b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/QRCodeEncoder.java index d39df03..2fd5c04 100644 --- a/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/QRCodeEncoder.java +++ b/zxingscanner/src/main/java/com/mylhyl/zxing/scanner/encode/QRCodeEncoder.java @@ -27,7 +27,6 @@ import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; -import com.mylhyl.zxing.scanner.common.Contents; import java.util.ArrayList; import java.util.Collections; @@ -121,12 +120,12 @@ private void encodeQRCodeContents(QREncode.Builder build) { String organization = contactBundle .getString(ContactsContract.Intents.Insert.COMPANY); String address = contactBundle.getString(ContactsContract.Intents.Insert.POSTAL); - List phones = getAllBundleValues(contactBundle, Contents.PHONE_KEYS); - List phoneTypes = getAllBundleValues(contactBundle, Contents.PHONE_TYPE_KEYS); - List emails = getAllBundleValues(contactBundle, Contents.EMAIL_KEYS); - String url = contactBundle.getString(Contents.URL_KEY); + List phones = getAllBundleValues(contactBundle, ParserUriToVCard.PHONE_KEYS); + List phoneTypes = getAllBundleValues(contactBundle, ParserUriToVCard.PHONE_TYPE_KEYS); + List emails = getAllBundleValues(contactBundle, ParserUriToVCard.EMAIL_KEYS); + String url = contactBundle.getString(ParserUriToVCard.URL_KEY); List urls = url == null ? null : Collections.singletonList(url); - String note = contactBundle.getString(Contents.NOTE_KEY); + String note = contactBundle.getString(ParserUriToVCard.NOTE_KEY); ContactEncoder encoder = build.isUseVCard() ? new VCardContactEncoder() : new MECARDContactEncoder(); String[] encoded = encoder.encode(Collections.singletonList(name), organization,