alvin890101
alvin890101

Reputation: 57

Android add text to Picture and save

I want to place a Textiew to my picture and save it, but I'm not sure how to insert the text into the picture.

I can attach an image on my picture save it, and it works but now I want to insert a Textiew into the picture.

Here is my code:

PictureCallback cameraPictureCallbackJpeg = new PictureCallback() 
{  
@Override
public void onPictureTaken(byte[] data, Camera camera) 
{
  // TODO Auto-generated method stub   
  Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

  wid = cameraBitmap.getWidth();
  hgt = cameraBitmap.getHeight();

  Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(newImage);
  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
  Drawable drawable = getResources().getDrawable(R.drawable.love);
  drawable.setBounds(20, 20, 260, 160);
  drawable.draw(canvas);

  File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPicture/"); 
  storagePath.mkdirs(); 

  File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");

  try
  {
    FileOutputStream out = new FileOutputStream(myImage);
    newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


    out.flush();
    out.close();
  }
  catch(FileNotFoundException e)
  {
    Log.d("In Saving File", e + "");    
  }
  catch(IOException e)
  {
    Log.d("In Saving File", e + "");
  }

  camera.startPreview();

  drawable = null;

  newImage.recycle();
  newImage = null;

  cameraBitmap.recycle();
  cameraBitmap = null;
}
;
};

Upvotes: 3

Views: 5529

Answers (2)

Janardan Borada
Janardan Borada

Reputation: 11

Permissions external storage read and write in Manifest File.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:requestLegacyExternalStorage="true"
    android:requestRawExternalStorageAccess="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Sticker"
    tools:targetApi="31">

Android 13 - READ_EXTERNAL_STORAGE_PERMISSION

Below the is code xml File .

<com.google.android.material.circularreveal.CircularRevealLinearLayout
    android:id="@+id/imageLayout"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_gravity="center"
    android:background="@color/black"
    android:gravity="center">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Demo Text"
        android:textColor="@color/white"
        android:textSize="20sp" />

</com.google.android.material.circularreveal.CircularRevealLinearLayout>

<com.google.android.material.imageview.ShapeableImageView
    android:id="@+id/imageDownloadShow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" />

<Button
    android:id="@+id/downloadImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Download Image" />

Below the is code Java File .

  binding.downloadImage.setOnClickListener(v -> setDownloadImageCode());

    private void setDownloadImageCode() {
    binding.imageLayout.setDrawingCacheEnabled(true);
    binding.imageLayout.buildDrawingCache();
    Bitmap bitmapImage = binding.imageLayout.getDrawingCache();
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
    file = new File(file, "fileName.png");
    try {
        OutputStream stream = null;
        stream = new FileOutputStream(file);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.flush();
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Uri mImageUri = Uri.parse(file.getAbsolutePath());
    binding.imageDownloadShow.setImageURI(mImageUri);
}

Upvotes: 0

Geobits
Geobits

Reputation: 22342

If you just want "text", and not necessarily a TextView, you can draw text directly on the Canvas using drawText().

Just change it to something like:

  Canvas canvas = new Canvas(newImage);
  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
  canvas.drawText("some text here", x, y, myPaint);

  ...

  newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);

If you need it to be a TextView for sure, you can convert the view to a bitmap, then draw it on the canvas with drawBitmap(). See this answer for an example of how to convert it.

Upvotes: 1

Related Questions