In this video it shows the steps to add text over an existing image by reading it in Bitmap format. It creates a Canvas using the Bitmap and then calls drawText option to edit the bitmap file.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]
Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...
However, the main Java code is copied below also for reference:
package com.programmerworld.textonbitmapimage;
import static android.Manifest.permission.READ_MEDIA_IMAGES;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
ActivityCompat.requestPermissions(this,
new String[]{READ_MEDIA_IMAGES},
PackageManager.PERMISSION_GRANTED);
imageView = findViewById(R.id.imageView);
editText = findViewById(R.id.editTextText);
return insets;
});
}
public void buttonAddTextOverImage(View view){
try {
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolumes().get(0); // 0 is for internal storage.
File fileInput = new File(storageVolume.getDirectory().getPath() +
"/Download/flower.jpg");
File fileOutput = new File(storageVolume.getDirectory().getPath() +
"/Download/" + System.currentTimeMillis() + ".jpg");
Bitmap bitmapInputImage = BitmapFactory.decodeFile(fileInput.getPath());
Bitmap bitmapOutputImage = bitmapInputImage.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmapOutputImage);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
canvas.drawText(editText.getText().toString(),
5, 50, paint);
imageView.setImageBitmap(bitmapOutputImage);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmapOutputImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] bytesArray = byteArrayOutputStream.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
fileOutputStream.write(bytesArray);
fileOutputStream.close();
} catch (Exception e) {
editText.setText(e.toString());
}
}
}
--
On this page of the site you can watch the video online How to add custom text over a Bitmap image from your Android App? with a duration of hours minute second in good quality, which was uploaded by the user Programmer World 08 April 2024, share the link with friends and acquaintances, this video has already been watched 185 times on youtube and it was liked by 3 viewers. Enjoy your viewing!