在Android中向图片添加文本并保存

3

我想在我的图片上放置一个Textiew并保存它,但我不知道如何将文本插入图片中。

我可以在我的图片上附加图像并保存,这很有效,但现在我想将Textiew插入到图片中。

这是我的代码:

PictureCallback cameraPictureCallbackJpeg = new PictureCallback() 
{  
@Override
public void onPictureTaken(byte[] data, Camera camera) 
{
  // TODO Auto-generated method stub   
  Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

  wid = cameraBitmap.getWidth();
  hgt = cameraBitmap.getHeight();

  Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(newImage);
  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
  Drawable drawable = getResources().getDrawable(R.drawable.love);
  drawable.setBounds(20, 20, 260, 160);
  drawable.draw(canvas);

  File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPicture/"); 
  storagePath.mkdirs(); 

  File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");

  try
  {
    FileOutputStream out = new FileOutputStream(myImage);
    newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


    out.flush();
    out.close();
  }
  catch(FileNotFoundException e)
  {
    Log.d("In Saving File", e + "");    
  }
  catch(IOException e)
  {
    Log.d("In Saving File", e + "");
  }

  camera.startPreview();

  drawable = null;

  newImage.recycle();
  newImage = null;

  cameraBitmap.recycle();
  cameraBitmap = null;
}
;
};
2个回答

1
如果你只需要“文本”,而不一定需要 TextView,你可以直接在画布上使用 drawText() 绘制文本。
只需将其更改为以下内容:
  Canvas canvas = new Canvas(newImage);
  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
  canvas.drawText("some text here", x, y, myPaint);

  ...

  newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);

如果您确实需要它成为一个TextView,您可以将视图转换为位图,然后使用drawBitmap()在画布上绘制它。请参见this answer,了解如何进行转换的示例。

我应该在myPaint中输入什么? - alvin890101
Paint 允许您定义文本的外观,包括对齐方式、颜色、字体等。虽然您可能只需传递 null 参数,但我不知道默认的画笔是什么样子的,因为我从未用过它来处理文本。 - Geobits
@Geobits,“cameraBitmap”是什么? - Onur-Andros Ozbek

0
在清单文件中授予外部存储读写权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:requestLegacyExternalStorage="true"
    android:requestRawExternalStorageAccess="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Sticker"
    tools:targetApi="31">

Android 13 - READ_EXTERNAL_STORAGE_PERMISSION

以下是XML文件的代码。

<com.google.android.material.circularreveal.CircularRevealLinearLayout
    android:id="@+id/imageLayout"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_gravity="center"
    android:background="@color/black"
    android:gravity="center">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Demo Text"
        android:textColor="@color/white"
        android:textSize="20sp" />

</com.google.android.material.circularreveal.CircularRevealLinearLayout>

<com.google.android.material.imageview.ShapeableImageView
    android:id="@+id/imageDownloadShow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" />

<Button
    android:id="@+id/downloadImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Download Image" />

以下是Java文件的代码。
  binding.downloadImage.setOnClickListener(v -> setDownloadImageCode());

    private void setDownloadImageCode() {
    binding.imageLayout.setDrawingCacheEnabled(true);
    binding.imageLayout.buildDrawingCache();
    Bitmap bitmapImage = binding.imageLayout.getDrawingCache();
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
    file = new File(file, "fileName.png");
    try {
        OutputStream stream = null;
        stream = new FileOutputStream(file);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.flush();
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Uri mImageUri = Uri.parse(file.getAbsolutePath());
    binding.imageDownloadShow.setImageURI(mImageUri);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接