0

I use an intent to capture image using the phones stock camera app. In fact this is the sample code from the android developers site, on how to take photos simply, with some adjustments that fit my app's needs.

    public void dispatchTakePictureIntent(int actionCode) {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File f = null;
        try {
            f = setUpPhotoFile();
            mCurrentPhotoPath = f.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        } catch (IOException e) {
            e.printStackTrace();
            f = null;
            mCurrentPhotoPath = null;
        }
    startActivityForResult(takePictureIntent, actionCode);
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mImageBitmap = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
    } else {
        mAlbumStorageDirFactory = new BaseAlbumDirFactory();
    }
    dispatchTakePictureIntent(ACTION_TAKE_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (mCurrentPhotoPath != null) {
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            File f = new File(mCurrentPhotoPath);
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);
            mCurrentPhotoPath = null;
        }
        dispatchTakePictureIntent(ACTION_TAKE_PHOTO);
    }
    else{
    finish();
    }
}

and a method that watermarks a bitmap with a string.

  public static Bitmap mark(Bitmap src, String watermark) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setTextSize(18);
    paint.setAntiAlias(true);
    paint.setUnderlineText(true);
    canvas.drawText(watermark, 20, 25, paint);

    return result;
}

The problem is I can't get the bitmap to use this method.

Many thanks in advance!

Vissarionas
  • 21
  • 1
  • 4

1 Answers1

0

You are passing a file Uri to save the taken picture. So try this. I have Changed your onActivityResult method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
    if (mCurrentPhotoPath != null) {
        Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        Bitmap photo = Media.getBitmap(getContentResolver(), contentUri );
        photo=mark(photo, yourWatreStringHere);
        OutputStream fOut = null;
        fOut = new FileOutputStream(f);
        photo.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
        fOut.flush();
        fOut.close(); // do not forget to close the stream
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
        mCurrentPhotoPath = null;
    }
    dispatchTakePictureIntent(ACTION_TAKE_PHOTO);
}
else{
finish();
}
}
Praveena
  • 6,340
  • 2
  • 40
  • 53
  • Man, thanks for your patience. I have tried that, and it works without the mark() method. I have put it in an asynctask too and it works fine, but it rotates the image. I get it somehow. The problem must be in the mark() method. any suggestions? – Vissarionas Nov 26 '14 at 17:14
  • Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the EXIF data with the orientation that the photo should be viewed in. Look at this question http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android – Praveena Nov 26 '14 at 19:30