如何在 Android 中将文本绘制到图像上
How to draw text onto an image in Android
我已经尝试了无数种方法,但仍然没有达到预期的效果;我想要实现的是:
- 绘制一个矩形,它是屏幕的整个宽度和屏幕高度的大约 1/3,其中矩形具有 50% 可见性/alpha(因此可以在矩形下方看到图像)并且矩形将放在屏幕的最底部
- 在文本集中在矩形中间的矩形内放置一个多行字符串
以上是我希望实现的基本概念,但如果可能的话,我想生成一个地图,它是一个小正方形,位于矩形内的左侧 - 如何将其添加到位图 / canvas 也非常有用。
我当前的实现(没有正确的形状或文本大小)如下所示:
Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
Bitmap newBitmap = ImageUtils.drawMultilineTextToBitmapV2(MainActivity.this, bitmap, imageText);
...
public static Bitmap drawMultilineTextToBitmapV2(Context context, Bitmap bitmap, String text) {
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
Bitmap alteredBitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(alteredBitmap);
// Add image to canvas
Paint paint = new Paint();
canvas.drawBitmap(bitmap, 0, 0, paint);
// Add background for text
Paint p2 = new Paint();
p2.setStyle(Paint.Style.FILL);
p2.setColor(Color.GRAY);
p2.setAlpha(0x80);
// int padding = 50;
int padding = 1000;
Rect rect = new Rect(
canvas.getWidth() - padding, // Left
canvas.getHeight() - padding, // Top
padding, // Bottom
canvas.getWidth() + padding // Right
);
canvas.drawRect(rect, p2);
// Add text
paint.setColor(Color.WHITE);
paint.setTextSize(250);
canvas.drawText(text, bitmap.getWidth(), bitmap.getHeight(), paint);
canvas.save();
return alteredBitmap;
}
如有任何帮助,我们将不胜感激。
更新:
String saveFilePath = FileUtils.saveImageToInternalStorage(newBitmap, fileDirectory, fileName, dateObject);
...
位图/canvas 被保存到一个文件中,以便用户可以再次访问它。
public static String saveImageToInternalStorage(Bitmap finalBitmap, File fileDirectory, String fileName, Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm", Locale.UK);
String now = simpleDateFormat.format(date);
fileDirectory = new File(fileDirectory + DCA_FILE_PATH);
fileDirectory.mkdirs();
if (fileName.isEmpty()) {
DecimalFormat df = new DecimalFormat("000000");
int i = 1;
String startingNumber = df.format(i);
fileName = now + "-" + startingNumber + ".jpg";
} else {
fileName = now + "-" + fileName + ".jpg";
}
File file = new File(fileDirectory, fileName);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, "Saving Internal File to: " + file.getAbsolutePath());
return file.getAbsolutePath();
}
更新 2:
申请过程:
- 使用相机视图在应用内拍照
- 照片以字节形式返回并转换为位图
- 位图转换为 canvas,其中需要创建文本和矩形,然后再转换回位图
- 然后将位图另存为文件
- 用户单击文件以查看包含文本/矩形的照片
您可以通过使用相对布局(也可以通过框架)来做到这一点,您可以在布局中设置图像视图,并在其上方的任何位置添加正方形或任何其他布局 want.Something在您的 xml 布局中喜欢此代码:
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/bg_gv_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_gv_medical_guide_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/txt_category_title"
android:layout_gravity="center"
android:textColor="@color/colorWhite"
android:background="@drawable/bg_category_title_rounded_rectangle"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="15sp"
android:text=""
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
使用文本视图的可绘制背景,您可以随意控制屏幕的 (1/3) 或任何您想要的内容。希望对您有所帮助。
实现方式:
public static Bitmap viewToBitmap(Activity activity, Bitmap bitmap, String mapURL, String latLong, String dateTime) {
try {
AsyncTask asyncTask = new BackgroundUtils.setImageFromUrl().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mapURL);
Bitmap googleBitmap = (Bitmap) asyncTask.get();
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
Bitmap bmp = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bmp);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) activity.findViewById(R.id.relativeLayout);
View layout = inflater.inflate(R.layout.screenshot_content, root, false);
ImageView cameraView = (ImageView) layout.findViewById(R.id.cameraView);
ImageView mapView = (ImageView) layout.findViewById(R.id.mapView);
TextView latLongView = (TextView) layout.findViewById(R.id.latLongView);
TextView dateTimeView = (TextView) layout.findViewById(R.id.dateTimeView);
cameraView.setImageBitmap(bitmap);
mapView.setImageBitmap(googleBitmap);
latLongView.setText(latLong);
dateTimeView.setText(dateTime);
layout.setDrawingCacheEnabled(true);
layout.measure(View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
layout.draw(canvas);
return bmp;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我已经尝试了无数种方法,但仍然没有达到预期的效果;我想要实现的是:
- 绘制一个矩形,它是屏幕的整个宽度和屏幕高度的大约 1/3,其中矩形具有 50% 可见性/alpha(因此可以在矩形下方看到图像)并且矩形将放在屏幕的最底部
- 在文本集中在矩形中间的矩形内放置一个多行字符串
以上是我希望实现的基本概念,但如果可能的话,我想生成一个地图,它是一个小正方形,位于矩形内的左侧 - 如何将其添加到位图 / canvas 也非常有用。
我当前的实现(没有正确的形状或文本大小)如下所示:
Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
Bitmap newBitmap = ImageUtils.drawMultilineTextToBitmapV2(MainActivity.this, bitmap, imageText);
...
public static Bitmap drawMultilineTextToBitmapV2(Context context, Bitmap bitmap, String text) {
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
Bitmap alteredBitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(alteredBitmap);
// Add image to canvas
Paint paint = new Paint();
canvas.drawBitmap(bitmap, 0, 0, paint);
// Add background for text
Paint p2 = new Paint();
p2.setStyle(Paint.Style.FILL);
p2.setColor(Color.GRAY);
p2.setAlpha(0x80);
// int padding = 50;
int padding = 1000;
Rect rect = new Rect(
canvas.getWidth() - padding, // Left
canvas.getHeight() - padding, // Top
padding, // Bottom
canvas.getWidth() + padding // Right
);
canvas.drawRect(rect, p2);
// Add text
paint.setColor(Color.WHITE);
paint.setTextSize(250);
canvas.drawText(text, bitmap.getWidth(), bitmap.getHeight(), paint);
canvas.save();
return alteredBitmap;
}
如有任何帮助,我们将不胜感激。
更新:
String saveFilePath = FileUtils.saveImageToInternalStorage(newBitmap, fileDirectory, fileName, dateObject);
...
位图/canvas 被保存到一个文件中,以便用户可以再次访问它。
public static String saveImageToInternalStorage(Bitmap finalBitmap, File fileDirectory, String fileName, Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm", Locale.UK);
String now = simpleDateFormat.format(date);
fileDirectory = new File(fileDirectory + DCA_FILE_PATH);
fileDirectory.mkdirs();
if (fileName.isEmpty()) {
DecimalFormat df = new DecimalFormat("000000");
int i = 1;
String startingNumber = df.format(i);
fileName = now + "-" + startingNumber + ".jpg";
} else {
fileName = now + "-" + fileName + ".jpg";
}
File file = new File(fileDirectory, fileName);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, "Saving Internal File to: " + file.getAbsolutePath());
return file.getAbsolutePath();
}
更新 2:
申请过程:
- 使用相机视图在应用内拍照
- 照片以字节形式返回并转换为位图
- 位图转换为 canvas,其中需要创建文本和矩形,然后再转换回位图
- 然后将位图另存为文件
- 用户单击文件以查看包含文本/矩形的照片
您可以通过使用相对布局(也可以通过框架)来做到这一点,您可以在布局中设置图像视图,并在其上方的任何位置添加正方形或任何其他布局 want.Something在您的 xml 布局中喜欢此代码:
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/bg_gv_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_gv_medical_guide_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/txt_category_title"
android:layout_gravity="center"
android:textColor="@color/colorWhite"
android:background="@drawable/bg_category_title_rounded_rectangle"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="15sp"
android:text=""
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
使用文本视图的可绘制背景,您可以随意控制屏幕的 (1/3) 或任何您想要的内容。希望对您有所帮助。
实现方式:
public static Bitmap viewToBitmap(Activity activity, Bitmap bitmap, String mapURL, String latLong, String dateTime) {
try {
AsyncTask asyncTask = new BackgroundUtils.setImageFromUrl().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mapURL);
Bitmap googleBitmap = (Bitmap) asyncTask.get();
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
Bitmap bmp = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bmp);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) activity.findViewById(R.id.relativeLayout);
View layout = inflater.inflate(R.layout.screenshot_content, root, false);
ImageView cameraView = (ImageView) layout.findViewById(R.id.cameraView);
ImageView mapView = (ImageView) layout.findViewById(R.id.mapView);
TextView latLongView = (TextView) layout.findViewById(R.id.latLongView);
TextView dateTimeView = (TextView) layout.findViewById(R.id.dateTimeView);
cameraView.setImageBitmap(bitmap);
mapView.setImageBitmap(googleBitmap);
latLongView.setText(latLong);
dateTimeView.setText(dateTime);
layout.setDrawingCacheEnabled(true);
layout.measure(View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
layout.draw(canvas);
return bmp;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}