我已经使用zxing从文本字符串创建了一个QRCode图像;现在我想在QRcode下面写两个字符串。我一次点击按钮就生成了许多QRCode。然后打印出来。我想知道在使用QRCode的打印形式时,哪个QRCode属于哪个文本。因此,需要在QRCode图像下方添加2个字符串。如何将其添加到QRCode jpeg图像中
生成QRCode图像的代码:
活动中:
Bitmap myBitmap = TextToImageEncode(total_text);
saveImage(myBitmap,busNumber, imageName, imagePath);
功能:
Bitmap TextToImageEncode(String QRText) {
MultiFormatWriter multiFormatWriter;
BitMatrix bitMatrix;
Bitmap bitmap = null;
multiFormatWriter = new MultiFormatWriter();
try {
bitMatrix = multiFormatWriter.encode(QRText, BarcodeFormat.QR_CODE, 200, 200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(bitMatrix);
} catch (WriterException e) {
e.printStackTrace();
}
return bitmap;
}
@RequiresApi(api = Build.VERSION_CODES.N)
public void saveImage(Bitmap myBitmap, String busNumber, String imageName, EditText imagePath) {
String IMAGE_DIRECTORY = "QRCode";
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY+"/"+busNumber );
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
Log.d("dirrrrrr", "" + wallpaperDirectory.mkdirs());
wallpaperDirectory.mkdirs();
}
try {
imagePath.setText("Chintu");
File f = new File(wallpaperDirectory,imageName + ".jpeg");
imagePath.setText("Sandeep");
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
imagePath.setText("SDK>23,has no permission");
f.createNewFile();
} else {
//do here
imagePath.setText("SDK>23,has no permission");
f.createNewFile();
}
} else {
//do here
imagePath.setText("SDK<23");
f.createNewFile();
}
//give read write permission
imagePath.setText("Chintu1");
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
imagePath.setText("f.getAbsolutePath()");
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
Toast.makeText(getBaseContext(), f.getAbsolutePath(), Toast.LENGTH_SHORT).show();
//return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
//imagePath.setText("Pintu");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do here
Toast.makeText(mContext, "The app was allowed to read your store.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, "The app was not allowed to read your store.", Toast.LENGTH_LONG).show();
}
}
}
}
使用代码完成:
Bitmap TextToImageEncode(String QRText, String imageName) {
MultiFormatWriter multiFormatWriter;
BitMatrix bitMatrix;
Bitmap bitmap = null;
int textWidth = 10;
multiFormatWriter = new MultiFormatWriter();
try {
bitMatrix = multiFormatWriter.encode(QRText, BarcodeFormat.QR_CODE, 200, 200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(bitMatrix);
Canvas canvas = new Canvas(bitmap);
// new antialiased Paint
TextPaint paint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(0, 0, 0));
// text size in pixels
paint.setTextSize(textWidth);
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// get position of text's top left corner
float x = 3 * textWidth;
float y = (bitmap.getHeight() - textWidth);
// draw text to the Canvas center
canvas.drawText(imageName,x,y,paint);
canvas.save();
canvas.restore();
} catch (WriterException e) {
e.printStackTrace();
}
return bitmap;
}