Home > Blockchain >  How can I print text on an image? | Android Studio | Java
How can I print text on an image? | Android Studio | Java

Time:01-27

I want to be able to print text on an image in my android app.

Currently, the code below allows the picture and text to be shared separately from each other. Please refer to the picture to see the result. How can I get the text to print on the image?

Please help me out. I would really appreciate.

holder.shareBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        bitmapDrawable = (BitmapDrawable) holder.imageView.getDrawable();// get the from imageview or use your drawable from drawable folder
        bitmap1 = bitmapDrawable.getBitmap();
        String imgBitmapPath= MediaStore.Images.Media.insertImage(context.getContentResolver(),
                bitmap1,"title",null);
        Uri imgBitmapUri=Uri.parse(imgBitmapPath);
        String shareText=paheliItem.getTitle();
        Intent shareIntent=new Intent(Intent.ACTION_SEND);
        shareIntent.setType("*/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
        context.startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));

    }
});

Image- result on clicking shareBtn

CodePudding user response:

see code below:

private void drawTextOnBitmap(Bitmap bmp, String text, int x, int y, int textColor) {
        Canvas canvas = new Canvas(bmp);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(textColor);
        // draw text
        canvas.drawText(text, x, y, paint);
    }


holder.shareBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        bitmapDrawable = (BitmapDrawable) holder.imageView.getDrawable();// get the from imageview or use your drawable from drawable folder
        // draw text on bitmap
        bitmap1 = drawTextOnBitmap(bitmapDrawable.getBitmap(), "YOUR TEXT", 0, 15, Color.RED);


        String imgBitmapPath= MediaStore.Images.Media.insertImage(context.getContentResolver(),
                bitmap1,"title",null);
        Uri imgBitmapUri=Uri.parse(imgBitmapPath);
        String shareText=paheliItem.getTitle();
        Intent shareIntent=new Intent(Intent.ACTION_SEND);
        shareIntent.setType("*/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
        context.startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));

    }
});
  •  Tags:  
  • Related