在 Android 上扫描并重新创建二维码
Scan and recreate QR code on Android
有谁知道可以在 Android 上扫描并重新创建 QR 码的方法(最好是单个应用程序)?
例如。我将我的 phone 指向代码,然后我可以在屏幕上显示该代码,这样它就可以被其他设备再次扫描。这与简单地拍摄 QR 码照片不同,因为我需要更长的时间才能拍到一张漂亮的代码照片,即使我这样做了,质量仍然很差,而且用其他设备扫描代码也需要很长时间.
尝试 zxing 库,https://github.com/zxing/zxing
1.将扫描后的字符串数据存储为位图:
public static Bitmap encodeAsBitmap(final String contentAfterScan, final BarcodeFormat format,
final int width, final int height) throws WriterException {
MultiFormatWriter writer = new MultiFormatWriter();
EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = writer.encode(contentAfterScan, format, width, height, hint);
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? 0xFF000000/*BLACK*/ : 0xFFFFFFFF/*WHITE*/;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
2。将图像存储到您的文件夹中
private void storeImageFromBitmap(Bitmap bitmap, String yourFolder, String imageName) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File f = new File(yourFolder + File.separator + imageName);
FileOutputStream fo;
try {
f.createNewFile();
fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
有谁知道可以在 Android 上扫描并重新创建 QR 码的方法(最好是单个应用程序)?
例如。我将我的 phone 指向代码,然后我可以在屏幕上显示该代码,这样它就可以被其他设备再次扫描。这与简单地拍摄 QR 码照片不同,因为我需要更长的时间才能拍到一张漂亮的代码照片,即使我这样做了,质量仍然很差,而且用其他设备扫描代码也需要很长时间.
尝试 zxing 库,https://github.com/zxing/zxing
1.将扫描后的字符串数据存储为位图:
public static Bitmap encodeAsBitmap(final String contentAfterScan, final BarcodeFormat format,
final int width, final int height) throws WriterException {
MultiFormatWriter writer = new MultiFormatWriter();
EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = writer.encode(contentAfterScan, format, width, height, hint);
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? 0xFF000000/*BLACK*/ : 0xFFFFFFFF/*WHITE*/;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
2。将图像存储到您的文件夹中
private void storeImageFromBitmap(Bitmap bitmap, String yourFolder, String imageName) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File f = new File(yourFolder + File.separator + imageName);
FileOutputStream fo;
try {
f.createNewFile();
fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}