ZXing Android embeddedを入れる
compile 'com.journeyapps:zxing-android-embedded:3.5.0'
そしてこれ。
private Bitmap createLogoQR(String text, @DrawableRes int drawableId, int width, int height) {
try {
BarcodeEncoder encoder = new BarcodeEncoder();
Map<EncodeHintType, Object> hints = new HashMap<>();
// 誤り訂正レベルHighで、30%程度QRコードが隠れていても読める
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
Bitmap qr = encoder.encodeBitmap(text, BarcodeFormat.QR_CODE, width, height, hints);
Bitmap logo = ((BitmapDrawable) ContextCompat.getDrawable(this, drawableId)).getBitmap();
Bitmap logoQr = Bitmap.createBitmap(qr.getWidth(), qr.getHeight(), qr.getConfig());
Canvas canvas = new Canvas(logoQr);
canvas.drawBitmap(qr, new Matrix(), null);
// 縦横30%程度のサイズにロゴを縮小する
int scaledLogoWidth = (int)(qr.getWidth() * 0.3f);
int scaledLogoHeight = (int)(qr.getHeight() * 0.3f);
Bitmap scaledLogo = Bitmap.createScaledBitmap(logo, scaledLogoWidth, scaledLogoHeight, true);
float cx = (qr.getWidth() - scaledLogoWidth) / 2.0f;
float cy = (qr.getHeight() - scaledLogoHeight) / 2.0f;
canvas.drawBitmap(scaledLogo, cx, cy, null);
return logoQr;
} catch (WriterException e) {
// コンテンツがエンコードできなかった場合
e.printStackTrace();
}
return null;
}