簡體   English   中英

如何在Android中將文字繪制到圖像上

[英]How to draw text onto an image in Android

我已經研究過無數種方法,但是仍然沒有達到預期的效果。 我想要實現的是:

  • 繪制一個矩形,該矩形是屏幕的整個寬度,大約是屏幕高度的1/3,其中矩形具有50%的可見性/ alpha(因此可以在矩形的下方看到圖像),並將矩形放置在屏幕底部
  • 在文本集中到矩形中間的矩形內放置多行字符串

上面是我要實現的基本概念,但是,如果可能的話,我想生成一個小矩形的地圖,該矩形位於矩形的左側,如何將其添加到位圖/畫布中也真的很有用。

我當前的實現(沒有正確的形狀或文本大小)如下所示:

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);

...

位圖/畫布將保存到文件中,因此用戶可以再次訪問它。

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:

申請流程:

  • 使用相機視圖在應用程序內拍照
  • 照片作為字節返回並轉換為位圖
  • 將位圖轉換為需要在其中創建文本和矩形的畫布,然后再轉換回為位圖
  • 然后將位圖保存為文件
  • 用戶單擊文件以查看包含文本/矩形的照片

您可以通過使用相對布局來做到這一點(也可以通過框架來實現),可以在布局中設置圖像視圖,並在任意位置添加正方形或任何其他布局在其上方。類似於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" />

使用textview的drawable bg,您可以按屏幕的(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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM