Skip to content

Commit

Permalink
1.jar包更新至最新版本3.3.3
Browse files Browse the repository at this point in the history
2.优化图片解析成功率
  • Loading branch information
yuzhiqiang1993 committed Dec 8, 2018
1 parent 81882be commit c9289f0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/com/yzq/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;
import com.yzq.zxinglibrary.encode.CodeCreator;

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:textStyle="bold" />
/>

<Button
android:id="@+id/scanBtn"
Expand Down
3 changes: 2 additions & 1 deletion zxinglibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api files('libs/core-3.3.0.jar')
api files('libs/core-3.3.3.jar')
compileOnly "com.android.support:appcompat-v7:$android_support"
compileOnly "com.android.support:design:$android_support"
compileOnly "com.android.support:support-v4:$android_support"
compileOnly "com.android.support:support-vector-drawable:$android_support"
// implementation 'com.google.zxing:core:3.3.2'

}
Binary file removed zxinglibrary/libs/core-3.3.0.jar
Binary file not shown.
Binary file added zxinglibrary/libs/core-3.3.3.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,8 @@ public void run() {
return;
}

/**
* 对图片进行裁剪,防止oom
*/
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小

scanBitmap = BitmapFactory.decodeFile(imgPath, options);


options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 400);
if (sampleSize <= 0) {
sampleSize = 1;
}
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(imgPath, options);
Bitmap scanBitmap = getBitmap(imgPath,400,400);

MultiFormatReader multiFormatReader = new MultiFormatReader();
// 解码的参数
Expand All @@ -70,7 +56,7 @@ public void run() {
}
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
// 设置解析的字符编码格式为UTF8
hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
// hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
// 设置解析配置参数
multiFormatReader.setHints(hints);
// 开始对图像资源解码
Expand All @@ -95,4 +81,44 @@ public void run() {
}



/**
* 根据路径获取图片
*
* @param filePath 文件路径
* @param maxWidth 图片最大宽度
* @param maxHeight 图片最大高度
* @return bitmap
*/
public static Bitmap getBitmap(final String filePath, final int maxWidth, final int maxHeight) {

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}


/**
* Return the sample size.
*
* @param options The options.
* @param maxWidth The maximum width.
* @param maxHeight The maximum height.
* @return the sample size
*/
private static int calculateInSampleSize(final BitmapFactory.Options options,
final int maxWidth,
final int maxHeight) {
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
while ((width >>= 1) >= maxWidth && (height >>= 1) >= maxHeight) {
inSampleSize <<= 1;
}
return inSampleSize;
}

}

0 comments on commit c9289f0

Please sign in to comment.