Add QrCodeGenerator for QrCode encoding
Bug: 118794858 Test: RunSettingsRoboTests Change-Id: I1e27c7b7bf97c1b683be7d200e1f3bbdc763c4c1
This commit is contained in:
47
src/com/android/settings/wifi/qrcode/QrCodeGenerator.java
Normal file
47
src/com/android/settings/wifi/qrcode/QrCodeGenerator.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.settings.wifi.qrcode;
|
||||||
|
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.Color;
|
||||||
|
|
||||||
|
import com.google.zxing.BarcodeFormat;
|
||||||
|
import com.google.zxing.MultiFormatWriter;
|
||||||
|
import com.google.zxing.WriterException;
|
||||||
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
|
||||||
|
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 BitMatrix qrBits = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,
|
||||||
|
size, size);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@@ -30,13 +30,10 @@ import android.view.SurfaceHolder;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
|
||||||
import com.google.zxing.BinaryBitmap;
|
import com.google.zxing.BinaryBitmap;
|
||||||
import com.google.zxing.LuminanceSource;
|
import com.google.zxing.LuminanceSource;
|
||||||
import com.google.zxing.RGBLuminanceSource;
|
import com.google.zxing.RGBLuminanceSource;
|
||||||
import com.google.zxing.MultiFormatWriter;
|
|
||||||
import com.google.zxing.WriterException;
|
import com.google.zxing.WriterException;
|
||||||
import com.google.zxing.common.BitMatrix;
|
|
||||||
import com.google.zxing.common.HybridBinarizer;
|
import com.google.zxing.common.HybridBinarizer;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
@@ -111,36 +108,17 @@ public class QrCameraTest {
|
|||||||
final String googleUrl = "http://www.google.com";
|
final String googleUrl = "http://www.google.com";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Bitmap bmp = encodeQrCode(googleUrl, 320);
|
final Bitmap bmp = QrCodeGenerator.encodeQrCode(googleUrl, 320);
|
||||||
int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
|
final int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
|
||||||
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
|
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
|
||||||
LuminanceSource source = new RGBLuminanceSource(bmp.getWidth(), bmp.getHeight(),
|
LuminanceSource source = new RGBLuminanceSource(bmp.getWidth(), bmp.getHeight(),
|
||||||
intArray);
|
intArray);
|
||||||
|
final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||||
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
|
||||||
mCamera.decodeImage(bitmap);
|
mCamera.decodeImage(bitmap);
|
||||||
|
bmp.recycle();
|
||||||
} catch (WriterException e) {
|
} catch (WriterException e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
assertThat(mQrCode).isEqualTo(googleUrl);
|
assertThat(mQrCode).isEqualTo(googleUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bitmap encodeQrCode(String qrCode, int size) throws WriterException {
|
|
||||||
BitMatrix qrBits = null;
|
|
||||||
try {
|
|
||||||
qrBits =
|
|
||||||
new MultiFormatWriter().encode(qrCode, BarcodeFormat.QR_CODE, size, size, null);
|
|
||||||
} catch (IllegalArgumentException iae) {
|
|
||||||
// Should never reach here.
|
|
||||||
}
|
|
||||||
assertThat(qrBits).isNotNull();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user