Joaquin Llaneza
Joaquin Llaneza

Reputation: 451

Add text to the full-size picture saved by the Android Camera Application and save as a new file without losing quality

I've been trying to improve this simple app which adds a text ("hola") to a picture taken by the Android Camera App and the saves the image to the sd card.

However, i have only managed to add the text to the thumbnail image from the returned data, and save the file to sd card.

Could anyone point me in the right direction to do the exact same thing but with the full-size photo?

Thanks a lot!!

What I have so far: (using code from tutorials and answers to similar questions on stackoverflow)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button captureBtn = (Button)findViewById(R.id.capture_btn);
    captureBtn.setOnClickListener(this);

}

public void onClick(View v) {
    if (v.getId() == R.id.capture_btn) {

        try {
            Intent tomaFotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(tomaFotoIntent, CAMERA_CAPTURE);
        } 
        catch(ActivityNotFoundException anfe){
            String errorMessage = "Device doesn't support capturing images";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //get the Uri for the captured image
            picUri = data.getData();
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //agregamos texto
            bmConTexto = writeTextOnDrawable(thePic, "hola");

            //guardamos la nueva imagen
            saveBitmap(bmConTexto.getBitmap());

            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(bmConTexto.getBitmap());
        }
    }
}

private BitmapDrawable writeTextOnDrawable(Bitmap thePic, String text) {

    Bitmap bm = thePic.copy(Bitmap.Config.ARGB_8888, true);
    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(20);

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);
    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset
    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;  
    canvas.drawText(text, xPos, yPos, paint);
    return new BitmapDrawable(getResources(), bm);


}

public void saveBitmap(Bitmap bm)
{
    try
    {
        String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
        String mFilePath = mBaseFolderPath + "abcd.jpg";

        FileOutputStream stream = new FileOutputStream(mFilePath);
        bm.compress(CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();
    }
    catch(Exception e)
    {
        Log.e("Could not save", e.toString());
    }
}

Upvotes: 0

Views: 1682

Answers (2)

Antrromet
Antrromet

Reputation: 15424

Your code seems correct only? What is the problem you are facing? Is the image you are getting from the camera thumbnail size? You could try using

Bitmap thePic = (Bitmap) data.getExtras().get("data"); 

instead of

Bitmap thePic = extras.getParcelable("data");

UPDATE:
Instead of returning a BitmapDrawable, you could directly return the Bitmap itself and check. Just change your last line to

return bm;

Upvotes: 0

Sevki
Sevki

Reputation: 3682

It sounds like you are trying to take a picture and write "Hola" on it, if this is the case have you tried looking in to takePicture() of the android camera api? If not can you elaborate the question?

Edit: pasted from the top of the page of the link above:

To take pictures with this class, use the following steps:

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
  4. If desired, call setDisplayOrientation(int).
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
  7. When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data.
  8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
  9. Call stopPreview() to stop updating the preview surface.
  10. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

Upvotes: 1

Related Questions