diff options
| -rw-r--r-- | packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.java | 63 | ||||
| -rw-r--r-- | packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.kt | 86 |
2 files changed, 86 insertions, 63 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.java b/packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.java deleted file mode 100644 index bc5824a4758d..000000000000 --- a/packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settingslib.qrcode; - -import android.graphics.Bitmap; -import android.graphics.Color; - -import com.google.zxing.BarcodeFormat; -import com.google.zxing.EncodeHintType; -import com.google.zxing.MultiFormatWriter; -import com.google.zxing.WriterException; -import com.google.zxing.common.BitMatrix; - -import java.nio.charset.CharsetEncoder; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - -public final class QrCodeGenerator { - /** - * Generates a barcode image with {@code contents}. - * - * @param contents The contents to encode in the barcode - * @param size The preferred image size in pixels - * @return Barcode bitmap - */ - public static Bitmap encodeQrCode(String contents, int size) - throws WriterException, IllegalArgumentException { - final Map<EncodeHintType, Object> hints = new HashMap<>(); - if (!isIso88591(contents)) { - hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name()); - } - - final BitMatrix qrBits = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, - size, size, hints); - final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565); - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - bitmap.setPixel(x, y, qrBits.get(x, y) ? Color.BLACK : Color.WHITE); - } - } - return bitmap; - } - - private static boolean isIso88591(String contents) { - CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder(); - return encoder.canEncode(contents); - } -} diff --git a/packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.kt b/packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.kt new file mode 100644 index 000000000000..7b67ec6d9bec --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/qrcode/QrCodeGenerator.kt @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.qrcode + +import android.annotation.ColorInt +import android.graphics.Bitmap +import android.graphics.Color +import com.google.zxing.BarcodeFormat +import com.google.zxing.EncodeHintType +import com.google.zxing.MultiFormatWriter +import com.google.zxing.WriterException +import java.nio.charset.StandardCharsets +import java.util.EnumMap + +object QrCodeGenerator { + /** + * Generates a barcode image with [contents]. + * + * @param contents The contents to encode in the barcode + * @param size The preferred image size in pixels + * @param invert Whether to invert the black/white pixels (e.g. for dark mode) + * @return Barcode bitmap + */ + @JvmStatic + @Throws(WriterException::class, java.lang.IllegalArgumentException::class) + fun encodeQrCode(contents: String, size: Int, invert: Boolean): Bitmap = + encodeQrCode(contents, size, DEFAULT_MARGIN, invert) + + private const val DEFAULT_MARGIN = -1 + + /** + * Generates a barcode image with [contents]. + * + * @param contents The contents to encode in the barcode + * @param size The preferred image size in pixels + * @param margin The margin around the actual barcode + * @param invert Whether to invert the black/white pixels (e.g. for dark mode) + * @return Barcode bitmap + */ + @JvmOverloads + @JvmStatic + @Throws(WriterException::class, IllegalArgumentException::class) + fun encodeQrCode( + contents: String, + size: Int, + margin: Int = DEFAULT_MARGIN, + invert: Boolean = false, + ): Bitmap { + val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java) + if (!isIso88591(contents)) { + hints[EncodeHintType.CHARACTER_SET] = StandardCharsets.UTF_8.name() + } + if (margin != DEFAULT_MARGIN) { + hints[EncodeHintType.MARGIN] = margin + } + val qrBits = MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, size, size, hints) + @ColorInt val setColor = if (invert) Color.WHITE else Color.BLACK + @ColorInt val unsetColor = if (invert) Color.BLACK else Color.WHITE + @ColorInt val pixels = IntArray(size * size) + for (x in 0 until size) { + for (y in 0 until size) { + pixels[x * size + y] = if (qrBits[x, y]) setColor else unsetColor + } + } + return Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565).apply { + setPixels(pixels, 0, size, 0, 0, size, size) + } + } + + private fun isIso88591(contents: String): Boolean = + StandardCharsets.ISO_8859_1.newEncoder().canEncode(contents) +} |