Android - 如何在保存之前为图像添加水印

时间:2014-11-23 09:27:57

标签: android image-processing watermark

我使用手机照相机应用程序捕获图像的意图。 事实上,这是来自Android开发者网站的示例代码,关于如何简单地拍摄照片,并进行一些适合我应用程序需求的调整。

    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();
    }
}

以及使用字符串为位图添加水印的方法。

  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;
}

问题是我无法让位图使用此方法。

非常感谢提前!

1 个答案:

答案 0 :(得分:0)

您正在传递文件Uri以保存拍摄的照片。所以试试吧。我更改了您的onActivityResult方法。

@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();
}
}