1、支持条形码下方显示Code。

2、优化扫条形码。
3、优化相机预览尺寸遍历策略、从而降低变形的可能性。
This commit is contained in:
jenly1314
2018-09-12 17:07:51 +08:00
parent cdb116200f
commit 06694d28a0
20 changed files with 463 additions and 49 deletions

Binary file not shown.

View File

@@ -36,17 +36,17 @@ ZXingLite for Android 是ZXing的精简版优化扫码和生成二维码功
<dependency> <dependency>
<groupId>com.king.zxing</groupId> <groupId>com.king.zxing</groupId>
<artifactId>zxing-lite</artifactId> <artifactId>zxing-lite</artifactId>
<version>1.0.1</version> <version>1.0.2</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
``` ```
### Gradle: ### Gradle:
```gradle ```gradle
implementation 'com.king.zxing:zxing-lite:1.0.1' implementation 'com.king.zxing:zxing-lite:1.0.2'
``` ```
### Lvy: ### Lvy:
```lvy ```lvy
<dependency org='com.king.zxing' name='zxing-lite' rev='1.0.1'> <dependency org='com.king.zxing' name='zxing-lite' rev='1.0.2'>
<artifact name='$AID' ext='pom'></artifact> <artifact name='$AID' ext='pom'></artifact>
</dependency> </dependency>
``` ```

Binary file not shown.

View File

@@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.king.zxing.app"> package="com.king.zxing.app">
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"

View File

@@ -47,7 +47,7 @@ public class CodeActivity extends AppCompatActivity {
if(isQRCode){ if(isQRCode){
createQRCode(getString(R.string.app_name)); createQRCode(getString(R.string.app_name));
}else{ }else{
createBarCode(getString(R.string.app_name)); createBarCode("1234567890");
} }
} }
@@ -69,7 +69,7 @@ public class CodeActivity extends AppCompatActivity {
*/ */
private void createBarCode(String content){ private void createBarCode(String content){
//生成条形码最好放子线程生成防止阻塞UI这里只是演示 //生成条形码最好放子线程生成防止阻塞UI这里只是演示
Bitmap bitmap = CodeUtils.createBarCode(content, BarcodeFormat.CODE_128,800,200); Bitmap bitmap = CodeUtils.createBarCode(content, BarcodeFormat.CODE_128,800,200,null,true);
//显示条形码 //显示条形码
ivCode.setImageBitmap(bitmap); ivCode.setImageBitmap(bitmap);
} }

View File

@@ -16,16 +16,26 @@
package com.king.zxing.app; package com.king.zxing.app;
import android.Manifest; import android.Manifest;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat; import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
import com.google.zxing.DecodeHintType;
import com.google.zxing.common.StringUtils;
import com.king.zxing.CaptureActivity; import com.king.zxing.CaptureActivity;
import com.king.zxing.Intents; import com.king.zxing.Intents;
import com.king.zxing.app.util.UriUtils;
import com.king.zxing.util.CodeUtils;
import java.util.List; import java.util.List;
@@ -37,10 +47,13 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
public static final String KEY_TITLE = "key_title"; public static final String KEY_TITLE = "key_title";
public static final String KEY_IS_QR_CODE = "key_code"; public static final String KEY_IS_QR_CODE = "key_code";
public static final int REQUEST_CODE = 0X01; public static final int REQUEST_CODE_SCAN = 0X01;
public static final int REQUEST_CODE_PHOTO = 0X02;
public static final int RC_CAMERA = 0X01; public static final int RC_CAMERA = 0X01;
public static final int RC_READ_PHOTO = 0X02;
private Class<?> cls; private Class<?> cls;
private String title; private String title;
@@ -53,15 +66,48 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){ if(resultCode == RESULT_OK && data!=null){
if(data!=null){ switch (requestCode){
//识别结果 case REQUEST_CODE_SCAN:
String result = data.getStringExtra(Intents.Scan.RESULT); String result = data.getStringExtra(Intents.Scan.RESULT);
Toast.makeText(this,result,Toast.LENGTH_SHORT).show(); Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
break;
case REQUEST_CODE_PHOTO:
parsePhoto(data);
break;
} }
} }
} }
private void parsePhoto(Intent data){
final String path = UriUtils.INSTANCE.getImagePath(this,data);
Log.d("Jenly","path:" + path);
if(TextUtils.isEmpty(path)){
return;
}
//异步解析
asyncThread(new Runnable() {
@Override
public void run() {
final String result = CodeUtils.parseCode(path);
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("Jenly","result:" + result);
Toast.makeText(getContext(),result,Toast.LENGTH_SHORT).show();
}
});
}
});
}
private Context getContext(){
return this;
}
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
@@ -73,7 +119,12 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
@Override @Override
public void onPermissionsGranted(int requestCode, List<String> list) { public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted // Some permissions have been granted
startScan(cls,title); switch (requestCode){
case RC_CAMERA:
startScan(cls,title);
break;
}
} }
@Override @Override
@@ -101,6 +152,10 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
} }
} }
private void asyncThread(Runnable runnable){
new Thread(runnable).start();
}
/** /**
* 扫码 * 扫码
* @param cls * @param cls
@@ -110,7 +165,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this,R.anim.in,R.anim.out); ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this,R.anim.in,R.anim.out);
Intent intent = new Intent(this, cls); Intent intent = new Intent(this, cls);
intent.putExtra(KEY_TITLE,title); intent.putExtra(KEY_TITLE,title);
ActivityCompat.startActivityForResult(this,intent,REQUEST_CODE,optionsCompat.toBundle()); ActivityCompat.startActivityForResult(this,intent,REQUEST_CODE_SCAN,optionsCompat.toBundle());
} }
/** /**
@@ -124,6 +179,24 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
startActivity(intent); startActivity(intent);
} }
private void startPhotoCode(){
Intent pickIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, REQUEST_CODE_PHOTO);
}
@AfterPermissionGranted(RC_READ_PHOTO)
private void checkExternalStoragePermissions(){
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {//有权限
startPhotoCode();
}else{
EasyPermissions.requestPermissions(this, getString(R.string.permission_external_storage),
RC_READ_PHOTO, perms);
}
}
public void OnClick(View v){ public void OnClick(View v){
switch (v.getId()){ switch (v.getId()){
case R.id.btn1: case R.id.btn1:
@@ -141,6 +214,9 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
case R.id.btn5: case R.id.btn5:
startCode(true); startCode(true);
break; break;
case R.id.btn6:
checkExternalStoragePermissions();
break;
} }
} }

View File

@@ -0,0 +1,69 @@
package com.king.zxing.app.util;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
/**
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public enum UriUtils {
INSTANCE;
/**
* 获取图片
*/
public String getImagePath(Context context,Intent data) {
String imagePath = null;
Uri uri = data.getData();
//获取系統版本
int currentapiVersion = Build.VERSION.SDK_INT;
if(currentapiVersion> Build.VERSION_CODES.KITKAT){
Log.d("uri=intent.getData :", "" + uri);
if (DocumentsContract.isDocumentUri(context, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
Log.d("getDocumentId(uri) :", "" + docId);
Log.d("uri.getAuthority() :", "" + uri.getAuthority());
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(context,MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getImagePath(context,contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
imagePath = getImagePath(context,uri, null);
}
}else{
imagePath = getImagePath(context,uri, null);
}
return imagePath;
}
/**
* 通过uri和selection来获取真实的图片路径,从相册获取图片时要用
*/
private String getImagePath(Context context,Uri uri, String selection) {
String path = null;
Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
}

View File

@@ -98,4 +98,17 @@
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn4" app:layout_constraintTop_toBottomOf="@+id/btn4"
style="@style/OnClick"/> style="@style/OnClick"/>
<Button
android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:text="识别一维码/二维码图片"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn5"
style="@style/OnClick"/>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

View File

@@ -1,6 +1,7 @@
<resources> <resources>
<string name="app_name">ZXingLite</string> <string name="app_name">ZXingLite</string>
<string name="permission_camera">App扫码需要用到拍摄权限</string> <string name="permission_camera">App扫码需要用到拍摄权限</string>
<string name="permission_external_storage">App需要用到读写权限</string>
<string name="tips_scan_code">将二维码/条形码放入框内,即可自动扫码</string> <string name="tips_scan_code">将二维码/条形码放入框内,即可自动扫码</string>
<string name="qr_code">二维码</string> <string name="qr_code">二维码</string>
<string name="bar_code">条形码</string> <string name="bar_code">条形码</string>

View File

@@ -5,12 +5,10 @@
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/> <uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- unavailable in API 23 --> <!-- unavailable in API 23 -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/> <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application> <application>
<activity <activity

View File

@@ -167,12 +167,10 @@ public class CaptureActivity extends Activity implements SurfaceHolder.Callback
handler = null; handler = null;
lastResult = null; lastResult = null;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (prefs.getBoolean(Preferences.KEY_DISABLE_AUTO_ORIENTATION, true)) {
setRequestedOrientation(getCurrentOrientation());
} else { } else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} }
resetStatusView(); resetStatusView();

View File

@@ -28,17 +28,17 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
final class DecodeFormatManager { public final class DecodeFormatManager {
private static final Pattern COMMA_PATTERN = Pattern.compile(","); private static final Pattern COMMA_PATTERN = Pattern.compile(",");
static final Set<BarcodeFormat> PRODUCT_FORMATS; public static final Set<BarcodeFormat> PRODUCT_FORMATS;
static final Set<BarcodeFormat> INDUSTRIAL_FORMATS; public static final Set<BarcodeFormat> INDUSTRIAL_FORMATS;
private static final Set<BarcodeFormat> ONE_D_FORMATS; public static final Set<BarcodeFormat> ONE_D_FORMATS;
static final Set<BarcodeFormat> QR_CODE_FORMATS = EnumSet.of(BarcodeFormat.QR_CODE); public static final Set<BarcodeFormat> QR_CODE_FORMATS = EnumSet.of(BarcodeFormat.QR_CODE);
static final Set<BarcodeFormat> DATA_MATRIX_FORMATS = EnumSet.of(BarcodeFormat.DATA_MATRIX); public static final Set<BarcodeFormat> DATA_MATRIX_FORMATS = EnumSet.of(BarcodeFormat.DATA_MATRIX);
static final Set<BarcodeFormat> AZTEC_FORMATS = EnumSet.of(BarcodeFormat.AZTEC); public static final Set<BarcodeFormat> AZTEC_FORMATS = EnumSet.of(BarcodeFormat.AZTEC);
static final Set<BarcodeFormat> PDF417_FORMATS = EnumSet.of(BarcodeFormat.PDF_417); public static final Set<BarcodeFormat> PDF417_FORMATS = EnumSet.of(BarcodeFormat.PDF_417);
static { static {
PRODUCT_FORMATS = EnumSet.of(BarcodeFormat.UPC_A, PRODUCT_FORMATS = EnumSet.of(BarcodeFormat.UPC_A,
BarcodeFormat.UPC_E, BarcodeFormat.UPC_E,

View File

@@ -74,7 +74,15 @@ final class DecodeHandler extends Handler {
private void decode(byte[] data, int width, int height) { private void decode(byte[] data, int width, int height) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Result rawResult = null; Result rawResult = null;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height); byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;
width = height;
height = tmp;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
if (source != null) { if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try { try {

View File

@@ -32,7 +32,7 @@ import com.google.zxing.DecodeHintType;
/** /**
* @author Lachezar Dobrev * @author Lachezar Dobrev
*/ */
final class DecodeHintManager { public final class DecodeHintManager {
private static final String TAG = DecodeHintManager.class.getSimpleName(); private static final String TAG = DecodeHintManager.class.getSimpleName();

View File

@@ -85,6 +85,7 @@ public final class ViewfinderView extends View {
private final float labelTextSize; private final float labelTextSize;
public static int scannerStart = 0; public static int scannerStart = 0;
public static int scannerEnd = 0; public static int scannerEnd = 0;
private boolean isShowResultPoint;
private List<ResultPoint> possibleResultPoints; private List<ResultPoint> possibleResultPoints;
private List<ResultPoint> lastPossibleResultPoints; private List<ResultPoint> lastPossibleResultPoints;
@@ -320,7 +321,15 @@ public final class ViewfinderView extends View {
invalidate(); invalidate();
} }
// /** public boolean isShowResultPoint() {
return isShowResultPoint;
}
public void setShowResultPoint(boolean showResultPoint) {
isShowResultPoint = showResultPoint;
}
// /**
// * Draw a bitmap with the result points highlighted instead of the live scanning display. // * Draw a bitmap with the result points highlighted instead of the live scanning display.
// * // *
// * @param barcode An image of the decoded barcode. // * @param barcode An image of the decoded barcode.
@@ -331,15 +340,18 @@ public final class ViewfinderView extends View {
// } // }
public void addPossibleResultPoint(ResultPoint point) { public void addPossibleResultPoint(ResultPoint point) {
List<ResultPoint> points = possibleResultPoints; if(isShowResultPoint){
synchronized (points) { List<ResultPoint> points = possibleResultPoints;
points.add(point); synchronized (points) {
int size = points.size(); points.add(point);
if (size > MAX_RESULT_POINTS) { int size = points.size();
// trim it if (size > MAX_RESULT_POINTS) {
points.subList(0, size - MAX_RESULT_POINTS / 2).clear(); // trim it
points.subList(0, size - MAX_RESULT_POINTS / 2).clear();
}
} }
} }
} }
} }

View File

@@ -123,6 +123,7 @@ final class CameraConfigurationManager {
display.getSize(theScreenResolution); display.getSize(theScreenResolution);
screenResolution = theScreenResolution; screenResolution = theScreenResolution;
Log.i(TAG, "Screen resolution in current orientation: " + screenResolution); Log.i(TAG, "Screen resolution in current orientation: " + screenResolution);
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution); cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
Log.i(TAG, "Camera resolution: " + cameraResolution); Log.i(TAG, "Camera resolution: " + cameraResolution);
bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution); bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
@@ -157,6 +158,9 @@ final class CameraConfigurationManager {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
theCamera.setDisplayOrientation(90);
theCamera.setParameters(parameters);
initializeTorch(parameters, prefs, safeMode); initializeTorch(parameters, prefs, safeMode);
CameraConfigurationUtils.setFocus( CameraConfigurationUtils.setFocus(

View File

@@ -25,6 +25,7 @@ import android.util.Log;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -44,7 +45,7 @@ public final class CameraConfigurationUtils {
private static final int MIN_PREVIEW_PIXELS = 480 * 320; // normal screen private static final int MIN_PREVIEW_PIXELS = 480 * 320; // normal screen
private static final float MAX_EXPOSURE_COMPENSATION = 1.5f; private static final float MAX_EXPOSURE_COMPENSATION = 1.5f;
private static final float MIN_EXPOSURE_COMPENSATION = 0.0f; private static final float MIN_EXPOSURE_COMPENSATION = 0.0f;
private static final double MAX_ASPECT_DISTORTION = 0.15; private static final double MAX_ASPECT_DISTORTION = 0.10;
private static final int MIN_FPS = 10; private static final int MIN_FPS = 10;
private static final int MAX_FPS = 20; private static final int MAX_FPS = 20;
private static final int AREA_PER_1000 = 400; private static final int AREA_PER_1000 = 400;
@@ -270,7 +271,7 @@ public final class CameraConfigurationUtils {
} }
} }
public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) { public static Point findBestPreviewSizeValue(Camera.Parameters parameters,final Point screenResolution) {
List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes(); List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
if (rawSupportedSizes == null) { if (rawSupportedSizes == null) {
@@ -282,6 +283,7 @@ public final class CameraConfigurationUtils {
return new Point(defaultSize.width, defaultSize.height); return new Point(defaultSize.width, defaultSize.height);
} }
if (Log.isLoggable(TAG, Log.INFO)) { if (Log.isLoggable(TAG, Log.INFO)) {
StringBuilder previewSizesString = new StringBuilder(); StringBuilder previewSizesString = new StringBuilder();
for (Camera.Size size : rawSupportedSizes) { for (Camera.Size size : rawSupportedSizes) {
@@ -291,7 +293,7 @@ public final class CameraConfigurationUtils {
} }
double screenAspectRatio = screenResolution.x / (double) screenResolution.y; double screenAspectRatio = screenResolution.x / (double) screenResolution.y;
Log.i(TAG, "screenAspectRatio: " + screenAspectRatio);
// Find a suitable size, with max resolution // Find a suitable size, with max resolution
int maxResolution = 0; int maxResolution = 0;
Camera.Size maxResPreviewSize = null; Camera.Size maxResPreviewSize = null;
@@ -304,10 +306,13 @@ public final class CameraConfigurationUtils {
} }
boolean isCandidatePortrait = realWidth < realHeight; boolean isCandidatePortrait = realWidth < realHeight;
int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth; int maybeFlippedWidth = isCandidatePortrait ? realWidth: realHeight ;
int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight; int maybeFlippedHeight = isCandidatePortrait ? realHeight : realWidth;
Log.i(TAG, String.format("maybeFlipped:%d * %d",maybeFlippedWidth,maybeFlippedHeight));
double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight; double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;
Log.i(TAG, "aspectRatio: " + aspectRatio);
double distortion = Math.abs(aspectRatio - screenAspectRatio); double distortion = Math.abs(aspectRatio - screenAspectRatio);
Log.i(TAG, "distortion: " + distortion);
if (distortion > MAX_ASPECT_DISTORTION) { if (distortion > MAX_ASPECT_DISTORTION) {
continue; continue;
} }
@@ -325,6 +330,21 @@ public final class CameraConfigurationUtils {
} }
} }
if (!rawSupportedSizes.isEmpty()) {
Collections.sort(rawSupportedSizes, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size o1, Camera.Size o2) {
int delta1 = Math.abs(o1.height-screenResolution.x);
int delta2 = Math.abs(o2.height-screenResolution.x);
return delta1 - delta2;
}
});
Camera.Size bestPreview = rawSupportedSizes.get(0);
Point bestSize = new Point(bestPreview.width, bestPreview.height);
Log.i(TAG, "Using largest suitable bestSize: " + bestSize);
return bestSize;
}
// If no exact match, use largest preview size. This was not a great idea on older devices because // If no exact match, use largest preview size. This was not a great idea on older devices because
// of the additional computation needed. We're likely to get here on newer Android 4+ devices, where // of the additional computation needed. We're likely to get here on newer Android 4+ devices, where
// the CPU is much more powerful. // the CPU is much more powerful.

View File

@@ -271,6 +271,12 @@ public final class CameraManager {
// rect.right = rect.right * cameraResolution.x / screenResolution.x; // rect.right = rect.right * cameraResolution.x / screenResolution.x;
// rect.top = rect.top * cameraResolution.y / screenResolution.y; // rect.top = rect.top * cameraResolution.y / screenResolution.y;
// rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y; // rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
framingRectInPreview = rect; framingRectInPreview = rect;
} }
return framingRectInPreview; return framingRectInPreview;

View File

@@ -16,19 +16,36 @@
package com.king.zxing.util; package com.king.zxing.util;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType; import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter; import com.google.zxing.MultiFormatWriter;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.king.zxing.DecodeFormatManager;
import com.king.zxing.DecodeHintManager;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.Vector;
/** /**
@@ -144,17 +161,127 @@ public class CodeUtils {
canvas.drawBitmap(src, 0, 0, null); canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2); canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null); canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
canvas.save(Canvas.ALL_SAVE_FLAG); canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore(); canvas.restore();
} catch (Exception e) { } catch (Exception e) {
bitmap = null; bitmap = null;
e.getStackTrace(); e.printStackTrace();
} }
return bitmap; return bitmap;
} }
/**
* 解析二维码图片
* @param bitmapPath
* @return
*/
public static String parseQRCode(String bitmapPath) {
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
return parseQRCode(bitmapPath,hints);
}
/**
* 解析二维码图片
* @param bitmapPath
* @param hints
* @return
*/
public static String parseQRCode(String bitmapPath, Map<DecodeHintType,?> hints){
try {
Result result = new QRCodeReader().decode(getBinaryBitmap(compressBitmap(bitmapPath)), hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解析一维码/二维码图片
* @param bitmapPath
* @return
*/
public static String parseCode(String bitmapPath){
Map<DecodeHintType,Object> hints = new HashMap<>();
//添加可以解析的编码类型
Vector<BarcodeFormat> decodeFormats = new Vector<>();
decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
decodeFormats.addAll(DecodeFormatManager.AZTEC_FORMATS);
decodeFormats.addAll(DecodeFormatManager.PDF417_FORMATS);
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
return parseCode(bitmapPath,hints);
}
/**
* 解析一维码/二维码图片
* @param bitmapPath
* @param hints 解析编码类型
* @return
*/
public static String parseCode(String bitmapPath, Map<DecodeHintType,Object> hints){
try {
MultiFormatReader reader = new MultiFormatReader();
reader.setHints(hints);
Result result = reader.decodeWithState(getBinaryBitmap(compressBitmap(bitmapPath)));
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 压缩图片
* @param path
* @return
*/
private static Bitmap compressBitmap(String path){
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;
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
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);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
// 重新读入图片注意此时已经把options.inJustDecodeBounds 设回false了
newOpts.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, newOpts);
}
/**
* 获取二进制图片
* @param bitmap
* @return
*/
private static BinaryBitmap getBinaryBitmap(@NonNull Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
//得到二进制图片
return new BinaryBitmap(new HybridBinarizer(source));
}
/** /**
* 生成条形码 * 生成条形码
* @param content * @param content
@@ -167,6 +294,7 @@ public class CodeUtils {
return createBarCode(content,format,desiredWidth,desiredHeight,null); return createBarCode(content,format,desiredWidth,desiredHeight,null);
} }
/** /**
* 生成条形码 * 生成条形码
* @param content * @param content
@@ -176,7 +304,37 @@ public class CodeUtils {
* @param hints * @param hints
* @return * @return
*/ */
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight,Map<EncodeHintType,?> hints) { public static Bitmap createBarCode(String content, BarcodeFormat format, int desiredWidth, int desiredHeight, Map<EncodeHintType,?> hints) {
return createBarCode(content,format,desiredWidth,desiredHeight,hints,false,40,Color.BLACK);
}
/**
* 生成条形码
* @param content
* @param format
* @param desiredWidth
* @param desiredHeight
* @param hints
* @param isShowText
* @return
*/
public static Bitmap createBarCode(String content, BarcodeFormat format, int desiredWidth, int desiredHeight, Map<EncodeHintType,?> hints, boolean isShowText) {
return createBarCode(content,format,desiredWidth,desiredHeight,hints,isShowText,40,Color.BLACK);
}
/**
* 生成条形码
* @param content
* @param format
* @param desiredWidth
* @param desiredHeight
* @param hints
* @param isShowText
* @param textSize
* @param textColor
* @return
*/
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight,Map<EncodeHintType,?> hints,boolean isShowText,int textSize,@ColorInt int textColor) {
if(TextUtils.isEmpty(content)){ if(TextUtils.isEmpty(content)){
return null; return null;
} }
@@ -201,6 +359,9 @@ public class CodeUtils {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888); Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height); bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
if(isShowText){
return addCode(bitmap,content,textSize,textColor,textSize/2);
}
return bitmap; return bitmap;
} catch (WriterException e) { } catch (WriterException e) {
e.printStackTrace(); e.printStackTrace();
@@ -208,4 +369,48 @@ public class CodeUtils {
return null; return null;
} }
/**
* 条形码下面添加文本信息
* @param src
* @param code
* @param textSize
* @param textColor
* @return
*/
private static Bitmap addCode(Bitmap src,String code,int textSize,@ColorInt int textColor,int offset) {
if (src == null) {
return null;
}
if (TextUtils.isEmpty(code)) {
return src;
}
//获取图片的宽高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
if (srcWidth <= 0 || srcHeight <= 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight + textSize + offset * 2, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
TextPaint paint = new TextPaint();
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(code,srcWidth/2,srcHeight + textSize /2 + offset,paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.printStackTrace();
}
return bitmap;
}
} }

View File

@@ -1,7 +1,7 @@
//App //App
def app_version = [:] def app_version = [:]
app_version.versionCode = 2 app_version.versionCode = 3
app_version.versionName = "1.0.1" app_version.versionName = "1.0.2"
ext.app_version = app_version ext.app_version = app_version
//build version //build version