Skip to content

Commit

Permalink
logo增加边框类型
Browse files Browse the repository at this point in the history
  • Loading branch information
hupei committed Nov 20, 2020
1 parent 023c7bb commit b92d2cf
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.zxing.client.result.ParsedResultType;
import com.mylhyl.zxing.scanner.common.Scanner;
import com.mylhyl.zxing.scanner.encode.QREncode;
import com.mylhyl.zxing.scanner.encode.QRLogoBorderType;

import java.io.ByteArrayOutputStream;

Expand Down Expand Up @@ -130,8 +131,9 @@ public void onClick(View v) {
// R.mipmap.connect_logo)
R.mipmap.wb_wlog_blow_bg_night)
)
.setLogoBorder(10)
.setLogoBorder(20)
.setLogoBorderColor(Color.GREEN)
.setLogoBorderType(QRLogoBorderType.CIRCLE)
.build()
.encodeAsBitmap();
imageView.setImageBitmap(bitmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
Expand Down Expand Up @@ -57,6 +60,44 @@ final class QRCodeEncoder {
private Context context;
private QREncode.Builder encodeBuild;

private static Bitmap getRoundedBitmap(Bitmap bitmap, float roundPx, int color) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

private static Bitmap getCircleBitmap(Bitmap bitmap, int color) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

QRCodeEncoder(QREncode.Builder build, Context context) {
this.context = context;
this.encodeBuild = build;
Expand Down Expand Up @@ -144,18 +185,41 @@ private Bitmap addLogo(Bitmap src) {
Bitmap bitmapLogo = Bitmap.createBitmap(srcLogo, 0, 0, logoWidth, logoHeight, matrix, false);

float logoBorder = encodeBuild.getLogoBorder();
// 先画边框
// 先画边框
if (logoBorder > 0) {
int logoBorderColor = encodeBuild.getLogoBorderColor();
int borderColor = encodeBuild.getLogoBorderColor() == -1 ? Color.WHITE : encodeBuild.getLogoBorderColor();
Bitmap borderBitmap = Bitmap.createBitmap((int) (bitmapLogo.getWidth() + logoBorder),
(int) (bitmapLogo.getHeight() + logoBorder), Bitmap.Config.ARGB_8888);
Canvas canvasLogoBorder = new Canvas(borderBitmap);
canvasLogoBorder.drawBitmap(srcLogo, 0, 0, null);
Canvas borderCanvas = new Canvas(borderBitmap);
borderCanvas.drawARGB(0, 0, 0, 0);

Paint borderPaint = new Paint();
borderPaint.setAntiAlias(true);
borderPaint.setColor(borderColor);

QRLogoBorderType logoBorderType = encodeBuild.getLogoBorderType();
Rect rectBorder = borderCanvas.getClipBounds();
// 矩形
if (logoBorderType == QRLogoBorderType.RECTANGLE) {
borderCanvas.drawRect(rectBorder, borderPaint);
}
// 圆形
else if (logoBorderType == QRLogoBorderType.CIRCLE) {
borderCanvas.drawOval(new RectF(rectBorder), borderPaint);
borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

bitmapLogo = getCircleBitmap(bitmapLogo, borderColor);
}
// 圆角矩形
else {
float logoBorderRadius = encodeBuild.getLogoBorderRadius();
borderCanvas.drawRoundRect(new RectF(rectBorder), logoBorderRadius, logoBorderRadius, borderPaint);
borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

// 原logo图片也画成圆角
bitmapLogo = getRoundedBitmap(bitmapLogo, logoBorderRadius, borderColor);
}

Rect rect = canvasLogoBorder.getClipBounds();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(logoBorderColor == -1 ? Color.WHITE : logoBorderColor);
canvasLogoBorder.drawRect(rect, paint);
canvas.drawBitmap(borderBitmap, left - (logoBorder / 2), top - (logoBorder / 2), null);
if (!borderBitmap.isRecycled()) {
borderBitmap.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public static class Builder {
private int logoSize;
private float logoBorder;
private int logoBorderColor = -1;
private QRLogoBorderType logoBorderType = QRLogoBorderType.ROUNDED;
private float logoBorderRadius = 30f;
private Bitmap qrBackground;
private int qrBackgroundColor;
private int margin = 4;
Expand Down Expand Up @@ -285,6 +287,38 @@ public Builder setLogoBorderColor(int color) {
return this;
}

QRLogoBorderType getLogoBorderType() {
return logoBorderType;
}

/**
* 二维码logo边框类型
*
* @param type 矩形,圆角矩形,圆形
* @return
* @since 2.1.8
*/
public Builder setLogoBorderType(QRLogoBorderType type) {
this.logoBorderType = type;
return this;
}

float getLogoBorderRadius() {
return logoBorderRadius;
}

/**
* 二维码logo边框圆角矩形半径
*
* @param radius 圆角半径,默认30
* @return
* @since 2.1.8
*/
public Builder setLogoBorderRadius(float radius) {
this.logoBorderRadius = radius;
return this;
}

/**
* @return
* @deprecated {@link #build()}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mylhyl.zxing.scanner.encode;

/**
* Created by hupei on 2020/11/20.
* <p>
* 功能描述:二维码logo边框枚举类型
*
* <ul>
* 业务逻辑说明:
* <li></li>
* </ul>
*
* <ul>
* 注意事项:
* <li></li>
* </ul>
*
* <ul>
* 变更记录:
* <li></li>
* </ul>
*
* @author hupei
* @since 2.1.8
*/
public enum QRLogoBorderType {
RECTANGLE, ROUNDED, CIRCLE
}

0 comments on commit b92d2cf

Please sign in to comment.