mauau
mauau

Reputation: 31

Text (date) stamp on photo

Is it possible merge text and photo taken from camera? I would like to stamp date and time on photo but I don't find anything on Google.

Upvotes: 3

Views: 7326

Answers (2)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Use below code to achieve what you required.

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.cuty); // the original file is cuty.jpg i added in resources
        Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system

        Canvas cs = new Canvas(dest);
        Paint tPaint = new Paint();
        tPaint.setTextSize(35);
        tPaint.setColor(Color.BLUE);
        tPaint.setStyle(Style.FILL);
        cs.drawBitmap(src, 0f, 0f, null);
        float height = tPaint.measureText("yY");
        cs.drawText(dateTime, 20f, height+15f, tPaint);
        try {
            dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

You have to use below permission in manifest file.

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

If you have any doubts feel free to ask. I am adding time stamp at top left corner of image, you can change it to anywhere on the image.

For my device the path is /sdcard to access external SD card, it may vary for other devices. Some devices may have /mnt/sdcard may be it is for internal sd cards. Just check it while before using this code snippet.

Upvotes: 8

bluefalcon
bluefalcon

Reputation: 4245

  • Create a bitmap object from the byte[] array that you get from JPEG picture callback using the method decodeByteArray
  • Create a canvas object using the bitmap you created above
  • Use the method drawText on the canvas object to write the text on the photo.

Upvotes: 2

Related Questions