Skip to content

Commit

Permalink
生成二维码异常更改为内部处理
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhiqiang1993 committed Dec 8, 2018
1 parent c9289f0 commit 815e1eb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
17 changes: 3 additions & 14 deletions app/src/main/java/com/yzq/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
Expand Down Expand Up @@ -142,13 +141,8 @@ public void onAction(List<String> permissions) {
return;
}

bitmap = CodeCreator.createQRCode(contentEtString, 400, 400, null);

try {
bitmap = CodeCreator.createQRCode(contentEtString, 400, 400, null);

} catch (WriterException e) {
e.printStackTrace();
}
if (bitmap != null) {
contentIv.setImageBitmap(bitmap);
}
Expand All @@ -163,14 +157,9 @@ public void onAction(List<String> permissions) {
return;
}

bitmap = null;
try {
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bitmap = CodeCreator.createQRCode(contentEtString, 400, 400, logo);
Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bitmap = CodeCreator.createQRCode(contentEtString, 400, 400, logo);

} catch (WriterException e) {
e.printStackTrace();
}
if (bitmap != null) {
contentIvWithLogo.setImageBitmap(bitmap);
}
Expand Down
2 changes: 1 addition & 1 deletion zxinglibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api files('libs/core-3.3.3.jar')
implementation 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
public class CodeCreator {



/*生成二维码*/
public static Bitmap createQRCode(String content, int w, int h,Bitmap logo) throws WriterException {
public static Bitmap createQRCode(String content, int w, int h, Bitmap logo) {


if (TextUtils.isEmpty(content)) {
Expand All @@ -29,13 +28,13 @@ public static Bitmap createQRCode(String content, int w, int h,Bitmap logo) thro
int offsetY = h / 2;

/*生成logo*/
Bitmap logoBitmap=null;
Bitmap logoBitmap = null;

if (logo!=null){
if (logo != null) {
Matrix matrix = new Matrix();
float scaleFactor = Math.min(w * 1.0f / 5 / logo.getWidth(), h * 1.0f / 5 /logo.getHeight());
matrix.postScale(scaleFactor,scaleFactor);
logoBitmap= Bitmap.createBitmap(logo, 0, 0, logo.getWidth(), logo.getHeight(), matrix, true);
float scaleFactor = Math.min(w * 1.0f / 5 / logo.getWidth(), h * 1.0f / 5 / logo.getHeight());
matrix.postScale(scaleFactor, scaleFactor);
logoBitmap = Bitmap.createBitmap(logo, 0, 0, logo.getWidth(), logo.getHeight(), matrix, true);
}


Expand All @@ -57,37 +56,48 @@ public static Bitmap createQRCode(String content, int w, int h,Bitmap logo) thro
//设置空白边距的宽度
hints.put(EncodeHintType.MARGIN, 0);
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, w, h, hints);

// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if(x >= offsetX && x < offsetX + logoW && y>= offsetY && y < offsetY + logoH){
int pixel = logoBitmap.getPixel(x-offsetX,y-offsetY);
if(pixel == 0){
if(matrix.get(x, y)){
pixel = 0xff000000;
}else{
pixel = 0xffffffff;
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, w, h, hints);

// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (x >= offsetX && x < offsetX + logoW && y >= offsetY && y < offsetY + logoH) {
int pixel = logoBitmap.getPixel(x - offsetX, y - offsetY);
if (pixel == 0) {
if (matrix.get(x, y)) {
pixel = 0xff000000;
} else {
pixel = 0xffffffff;
}
}
}
pixels[y * w + x] = pixel;
}else{
if (matrix.get(x, y)) {
pixels[y * w + x] = 0xff000000;
pixels[y * w + x] = pixel;
} else {
pixels[y * w + x] = 0xffffffff;
if (matrix.get(x, y)) {
pixels[y * w + x] = 0xff000000;
} else {
pixels[y * w + x] = 0xffffffff;
}
}
}
}

Bitmap bitmap = Bitmap.createBitmap(w, h,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;


} catch (WriterException e) {

System.out.print(e);
return null;
}

Bitmap bitmap = Bitmap.createBitmap(w, h,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;

}

}

0 comments on commit 815e1eb

Please sign in to comment.