How do I write text over a picture in Android and save it?

前端 未结 4 613
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:32

How can I write text on an image and then save it in Android?

Basically I want to let user write something on the images which my camera app will click for them. I c

4条回答
  •  悲&欢浪女
    2020-11-29 00:52

    Add text on bitmap :

    public Bitmap drawTextToBitmap(Context gContext, 
      int gResId, 
      String gText) {
      Resources resources = gContext.getResources();
      float scale = resources.getDisplayMetrics().density;
      Bitmap bitmap = 
          BitmapFactory.decodeResource(resources, gResId);
    
      android.graphics.Bitmap.Config bitmapConfig =
          bitmap.getConfig();
      // set default bitmap config if none
      if(bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
      }
      // resource bitmaps are imutable, 
      // so we need to convert it to mutable one
      bitmap = bitmap.copy(bitmapConfig, true);
    
      Canvas canvas = new Canvas(bitmap);
      // new antialised Paint
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
      // text color - #3D3D3D
      paint.setColor(Color.rgb(61, 61, 61));
      // text size in pixels
      paint.setTextSize((int) (14 * scale));
      // text shadow
      paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    
      // draw text to the Canvas center
      Rect bounds = new Rect();
      paint.getTextBounds(gText, 0, gText.length(), bounds);
      int x = (bitmap.getWidth() - bounds.width())/2;
      int y = (bitmap.getHeight() + bounds.height())/2;
    
      canvas.drawText(gText, x, y, paint);
    
      return bitmap;
    }
    

    Source : http://www.skoumal.net/en/android-how-draw-text-bitmap/

    Add multi line text on bitmap :

    public Bitmap drawMultilineTextToBitmap(Context gContext,
                                       int gResId,
                                       String gText) {
    
      // prepare canvas
      Resources resources = gContext.getResources();
      float scale = resources.getDisplayMetrics().density;
      Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
    
      android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
      // set default bitmap config if none
      if(bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
      }
      // resource bitmaps are imutable,
      // so we need to convert it to mutable one
      bitmap = bitmap.copy(bitmapConfig, true);
    
      Canvas canvas = new Canvas(bitmap);
    
      // new antialiased Paint
      TextPaint paint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
      // text color - #3D3D3D
      paint.setColor(Color.rgb(61, 61, 61));
      // text size in pixels
      paint.setTextSize((int) (14 * scale));
      // text shadow
      paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    
      // set text width to canvas width minus 16dp padding
      int textWidth = canvas.getWidth() - (int) (16 * scale);
    
      // init StaticLayout for text
      StaticLayout textLayout = new StaticLayout(
        gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
    
      // get height of multiline text
      int textHeight = textLayout.getHeight();
    
      // get position of text's top left corner
      float x = (bitmap.getWidth() - textWidth)/2;
      float y = (bitmap.getHeight() - textHeight)/2;
    
      // draw text to the Canvas center
      canvas.save();
      canvas.translate(x, y);
      textLayout.draw(canvas);
      canvas.restore();
    
      return bitmap;
    }
    

    Source : http://www.skoumal.net/en/android-drawing-multiline-text-on-bitmap/

提交回复
热议问题