1

In an android app i have photo capture functionality using custom camera view.I am using SurfaceHolder.Callback and able to capture image.But now i want to display some text on that captured image. How to do this. below is my code -

preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);

and callback method -

 PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            long time = 0;
            try {
                new File(filePath).createNewFile();
                outStream = new FileOutputStream(filePath);
                outStream.write(data);
                captureBtn.setVisibility(View.GONE); 
                preview.shutdownCamera();
                preview.setVisibility(View.GONE);
                outStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Log.d(TAG, "onPictureTaken - jpeg");
        }
    };

EDITED

I think my question is not clear,i want to add watermark on my captured image.Any help please.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68

3 Answers3

0
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(20); 
canvas.drawText("Some Text", 10, 25, paint); 
grig
  • 848
  • 6
  • 15
0

Check this code : First add ImageView and TextView in parent layout in which you set the image like this.

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(result.getWidth(), result.getHeight());
imgPreview.setLayoutParams(layoutParams);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(result.getWidth(), result.getHeight());
txtPreview.setLayoutParams(params);

Then add it in parent layout like this (rlPreview is a parent layout) :

txtPreview.setText("Enter text which you want to display in image");
txtPreview.setTextSize(5.0f);
rlPreview.addView(imgPreview);
rlPreview.addView(txtPreview);

and then, do this to create an image with text :

rlPreview.setDrawingCacheEnabled(true);
Bitmap bitmapWithWaterMark = Bitmap.createScaledBitmap( rlPreview.getDrawingCache(), rlPreview.getWidth(), rlPreview.getHeight(), true);
rlPreview.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bmp = bitmapWithWaterMark.compress(Bitmap.CompressFormat.PNG, 0, bytes);

There you go!! You have the image with text.

Deep Mehta
  • 1,237
  • 2
  • 11
  • 17
0

You can use this layout to display the image:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

After you display the image, you can set whatever text you want.

max59
  • 606
  • 1
  • 7
  • 16
  • i think you not got my question correctly,Basically i need a image with some text on it.When user capture image then captured image on sd card should have image with text on it.and before capturing text should not be display on surface view. – Ravi Bhandari Dec 24 '14 at 12:57