Skip to content

Commit

Permalink
[QRCode Android] QR code positioning in generated bitmap
Browse files Browse the repository at this point in the history
Bug: 1090600
Change-Id: Iee512f93de84aecba9f642326b1b78c8fddb2cf7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2241474
Reviewed-by: Kyle Milka <kmilka@chromium.org>
Commit-Queue: Gayane Petrosyan <gayane@chromium.org>
Cr-Commit-Position: refs/heads/master@{#777405}
  • Loading branch information
Gayane Petrosyan authored and Commit Bot committed Jun 11, 2020
1 parent d1bc5a4 commit 244401d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
8 changes: 4 additions & 4 deletions chrome/browser/share/android/java/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<dimen name="overlay_text_top_padding">14dp</dimen>

<!-- URL formatting in QR code bitmap -->
<dimen name="url_box_top_padding">128dp</dimen>
<dimen name="url_box_left_padding">40dp</dimen>
<dimen name="url_box_bottom_padding">32dp</dimen>
<dimen name="qrcode_size">212dp</dimen>
<dimen name="url_box_top_padding">70dp</dimen>
<dimen name="url_box_bottom_padding">25dp</dimen>
<dimen name="side_padding">50dp</dimen>
<dimen name="qrcode_size">200dp</dimen>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ public void onImageSaveError(String displayName) {
}

private Bitmap addUrlToBitmap(Bitmap bitmap, String url) {
// Assumes QR code bitmap is a square.
int qrCodeSize = mContext.getResources().getDimensionPixelSize(
org.chromium.chrome.browser.share.R.dimen.qrcode_size);
int fontSize = mContext.getResources().getDimensionPixelSize(R.dimen.text_size_large);
int textLeftPadding = mContext.getResources().getDimensionPixelSize(
org.chromium.chrome.browser.share.R.dimen.url_box_left_padding);
int sidePadding = mContext.getResources().getDimensionPixelSize(
org.chromium.chrome.browser.share.R.dimen.side_padding);
int textTopPadding = mContext.getResources().getDimensionPixelSize(
org.chromium.chrome.browser.share.R.dimen.url_box_top_padding);
int textBottomPadding = mContext.getResources().getDimensionPixelSize(
Expand All @@ -150,24 +149,22 @@ private Bitmap addUrlToBitmap(Bitmap bitmap, String url) {
mTextPaint.setColor(android.graphics.Color.BLACK);
mTextPaint.setTextSize(fontSize);

// New bitmap should have left and right margins of 25% of QR code bitmap.
int width = (int) (qrCodeSize * 1.5);
FixedLineCountLayout mTextLayout = new FixedLineCountLayout(url, mTextPaint,
width - textLeftPadding * 2, Alignment.ALIGN_CENTER, 1.0f, 0.0f, true, 2);
// Text is as wide as the QR code.
FixedLineCountLayout mTextLayout = new FixedLineCountLayout(
url, mTextPaint, qrCodeSize, Alignment.ALIGN_CENTER, 1.0f, 0.0f, true, 2);

// New bitmap should be long enough to fit the url with its margins, the QR code bitmap and
// 25% of it as bottom margin.
int height = textTopPadding + mTextLayout.getHeight() + textBottomPadding
+ (int) (qrCodeSize * 1.25);
// equal padding from the bottom.
int height =
(textTopPadding + mTextLayout.getHeight() + textBottomPadding) * 2 + qrCodeSize;
int width = qrCodeSize + 2 * sidePadding;
Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(android.graphics.Color.WHITE);
canvas.translate(textLeftPadding, textTopPadding);
canvas.translate(sidePadding, textTopPadding);
mTextLayout.draw(canvas);
canvas.drawBitmap(Bitmap.createScaledBitmap(bitmap, qrCodeSize, qrCodeSize, false),
(int) ((width - qrCodeSize) / 2) - textLeftPadding,
canvas.drawBitmap(Bitmap.createScaledBitmap(bitmap, qrCodeSize, qrCodeSize, false), 0,
mTextLayout.getHeight() + textBottomPadding, mTextPaint);

return newBitmap;
}

Expand Down

0 comments on commit 244401d

Please sign in to comment.