use of com.google.zxing.WriterException in project smartmodule by carozhu.
the class GenerateQrcodePIcHelper method createQRCodeWithLogo.
/**
* 生成带logo的二维码,logo默认为二维码的1/5
*
* @param text 需要生成二维码的文字、网址等
* @param size 需要生成二维码的大小()
* @param mBitmap logo文件
* @return bitmap
*/
public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap mBitmap) {
try {
IMAGE_HALFWIDTH = size / 10;
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
/*
* 设置容错级别,默认为ErrorCorrectionLevel.L
* 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了
*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
//矩阵高度
int width = bitMatrix.getWidth();
//矩阵宽度
int height = bitMatrix.getHeight();
int halfW = width / 2;
int halfH = height / 2;
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();
m.setScale(sx, sy);
//设置缩放信息
//将logo图片按martix设置的信息缩放
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
//该位置用于存放图片信息
//记录图片每个像素信息
pixels[y * width + x] = mBitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
} else {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
//灰色-- 0xEEEEEEEE md_grey_200
pixels[y * size + x] = 0xFFFFFFFF;
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
use of com.google.zxing.WriterException in project weex-example by KalicyZhou.
the class QRCodeEncoder method encodeFromStreamExtra.
// Handles send intents from the Contacts app, retrieving a contact as a VCARD.
private void encodeFromStreamExtra(Intent intent) throws WriterException {
format = BarcodeFormat.QR_CODE;
Bundle bundle = intent.getExtras();
if (bundle == null) {
throw new WriterException("No extras");
}
Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM);
if (uri == null) {
throw new WriterException("No EXTRA_STREAM");
}
byte[] vcard;
String vcardString;
InputStream stream = null;
try {
stream = activity.getContentResolver().openInputStream(uri);
if (stream == null) {
throw new WriterException("Can't open stream for " + uri);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = stream.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
}
vcard = baos.toByteArray();
vcardString = new String(vcard, 0, vcard.length, "UTF-8");
} catch (IOException ioe) {
throw new WriterException(ioe);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// continue
}
}
}
Log.d(TAG, "Encoding share intent content:");
Log.d(TAG, vcardString);
Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
ParsedResult parsedResult = ResultParser.parseResult(result);
if (!(parsedResult instanceof AddressBookParsedResult)) {
throw new WriterException("Result was not an address");
}
encodeQRCodeContents((AddressBookParsedResult) parsedResult);
if (contents == null || contents.isEmpty()) {
throw new WriterException("No content to encode");
}
}
use of com.google.zxing.WriterException in project weex-example by KalicyZhou.
the class EncodeActivity method share.
private void share() {
QRCodeEncoder encoder = qrCodeEncoder;
if (encoder == null) {
// Odd
Log.w(TAG, "No existing barcode to send?");
return;
}
String contents = encoder.getContents();
if (contents == null) {
Log.w(TAG, "No existing barcode to send?");
return;
}
Bitmap bitmap;
try {
bitmap = encoder.encodeAsBitmap();
} catch (WriterException we) {
Log.w(TAG, we);
return;
}
if (bitmap == null) {
return;
}
File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
File barcodesRoot = new File(bsRoot, "Barcodes");
if (!barcodesRoot.exists() && !barcodesRoot.mkdirs()) {
Log.w(TAG, "Couldn't make dir " + barcodesRoot);
showErrorMessage(R.string.msg_unmount_usb);
return;
}
File barcodeFile = new File(barcodesRoot, makeBarcodeFileName(contents) + ".png");
if (!barcodeFile.delete()) {
Log.w(TAG, "Could not delete " + barcodeFile);
// continue anyway
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(barcodeFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
} catch (FileNotFoundException fnfe) {
Log.w(TAG, "Couldn't access file " + barcodeFile + " due to " + fnfe);
showErrorMessage(R.string.msg_unmount_usb);
return;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
// do nothing
}
}
}
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " - " + encoder.getTitle());
intent.putExtra(Intent.EXTRA_TEXT, contents);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + barcodeFile.getAbsolutePath()));
intent.setType("image/png");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(Intent.createChooser(intent, null));
}
use of com.google.zxing.WriterException in project weex-example by KalicyZhou.
the class EncodeActivity method onResume.
@Override
protected void onResume() {
super.onResume();
// This assumes the view is full screen, which is a good assumption
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point displaySize = new Point();
display.getSize(displaySize);
int width = displaySize.x;
int height = displaySize.y;
int smallerDimension = width < height ? width : height;
smallerDimension = smallerDimension * 7 / 8;
Intent intent = getIntent();
if (intent == null) {
return;
}
try {
boolean useVCard = intent.getBooleanExtra(USE_VCARD_KEY, false);
qrCodeEncoder = new QRCodeEncoder(this, intent, smallerDimension, useVCard);
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
if (bitmap == null) {
Log.w(TAG, "Could not encode barcode");
showErrorMessage(R.string.msg_encode_contents_failed);
qrCodeEncoder = null;
return;
}
ImageView view = (ImageView) findViewById(R.id.image_view);
view.setImageBitmap(bitmap);
TextView contents = (TextView) findViewById(R.id.contents_text_view);
if (intent.getBooleanExtra(Intents.Encode.SHOW_CONTENTS, true)) {
contents.setText(qrCodeEncoder.getDisplayContents());
setTitle(qrCodeEncoder.getTitle());
} else {
contents.setText("");
setTitle("");
}
} catch (WriterException e) {
Log.w(TAG, "Could not encode barcode", e);
showErrorMessage(R.string.msg_encode_contents_failed);
qrCodeEncoder = null;
}
}
use of com.google.zxing.WriterException in project run-wallet-android by runplay.
the class QR method generateImage.
public static final Bitmap generateImage(final String text, Activity activity) {
if (text == null || text.trim().isEmpty()) {
return null;
}
int size = 390;
Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 1);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
try {
BitMatrix byteMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);
int height = byteMatrix.getHeight();
int width = byteMatrix.getWidth();
final Bitmap qrImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
qrImage.setPixel(x, y, byteMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
Bitmap background = Bitmap.createScaledBitmap(((BitmapDrawable) B.getDrawable(activity, R.drawable.qr_bg)).getBitmap(), 390, 90, false);
Bitmap bmOverlay = Bitmap.createBitmap(qrImage.getWidth(), qrImage.getHeight() + 90, qrImage.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(qrImage, 0, 90, null);
canvas.drawBitmap(background, 0, 0, null);
return bmOverlay;
} catch (WriterException e) {
Log.e("QR.ex", "" + e.getMessage());
}
return null;
}
Aggregations