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: programmerworld1990@gmail.com
Complete source code and other details:
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());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TextOnBitmapImage"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextText"
android:layout_width="252dp"
android:layout_height="80dp"
android:layout_marginStart="92dp"
android:layout_marginTop="29dp"
android:ems="10"
android:inputType="text"
android:text="Hello Programmer World!!!"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginTop="24dp"
android:onClick="buttonAddTextOverImage"
android:text="Add Text Over Image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextText" />
<ImageView
android:id="@+id/imageView"
android:layout_width="338dp"
android:layout_height="301dp"
android:layout_marginStart="34dp"
android:layout_marginTop="63dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
Complete project folder on payment of USD 9:
https://drive.google.com/file/d/1k7msyiS7o1zX3ANL0mOKrS3gQOpywzE4/view?usp=sharing
Excerpt:
Here’s a summary of the provided content:
The content includes a tutorial video demonstrating the process of adding text over an existing image by reading it in Bitmap format. It also provides the source code for an Android application that accomplishes this task. The code involves creating a Canvas using the Bitmap and then using the drawText option to edit the bitmap file. Furthermore, it offers links to contact the developers, view the complete project folder, and make a purchase, as well as images and a payment link.