diff --git a/app/src/main/java/com/yzq/zxing/MainActivity.java b/app/src/main/java/com/yzq/zxing/MainActivity.java index 30ca890..e7f118c 100644 --- a/app/src/main/java/com/yzq/zxing/MainActivity.java +++ b/app/src/main/java/com/yzq/zxing/MainActivity.java @@ -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; @@ -142,13 +141,8 @@ public void onAction(List 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); } @@ -163,14 +157,9 @@ public void onAction(List 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); } diff --git a/zxinglibrary/build.gradle b/zxinglibrary/build.gradle index 6d5013f..1259f6d 100644 --- a/zxinglibrary/build.gradle +++ b/zxinglibrary/build.gradle @@ -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" diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/encode/CodeCreator.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/encode/CodeCreator.java index 245b52f..4702aa7 100644 --- a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/encode/CodeCreator.java +++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/encode/CodeCreator.java @@ -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)) { @@ -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); } @@ -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; + } }