[英]How to draw text on captured image?
嗨,我正嘗試使用下面的代碼在捕獲的圖像上添加文本,之后,我想使用一個單獨的文件夾將該文本繪制圖像保存在設備內部存儲中。
但是使用下面的代碼,該文件夾未顯示在內部存儲中。
有人可以幫我做錯什么嗎?
/**
* onCaptureImageResult
*
* @param
*/
private void onCaptureImageResult() {
try {
File shopImageFile = new File(Utilities.getRealPathFromURI(imageUri, this));
Bitmap thumbnail = Bitmap.createScaledBitmap(BitmapFactory.decodeFile("" + shopImageFile),
100, 100, true);
saveImage(thumbnail);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
/**
* saveImage
*/
private void saveImage(Bitmap originalBitmap) {
//File myDir = new File("/sdcard/saved_images");
//myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
// String fname = "Image-" + n + ".jpg";
ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
//File file = new File(myDir, fname);
File file = wrapper.getDir("Images", MODE_PRIVATE);
if (file.exists()){
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
// NEWLY ADDED CODE STARTS HERE [
Canvas canvas = new Canvas(originalBitmap);
Paint paint = new Paint();
paint.setColor(Color.RED); // Text Color
paint.setStrokeWidth(12); // Text Size
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
// some more settings...
canvas.drawBitmap(originalBitmap, 0, 0, paint);
canvas.drawText("Testing...", 100, 100, paint);
// NEWLY ADDED CODE ENDS HERE ]
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
親愛的使用此庫在捕獲的圖像上繪制文本。
compile('com.tengio.android:graffiti:latest_version')
像這樣打開圖像
Uri uri = new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
.path(image path)
.build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.