裁剪图像和水印它

时间:2014-12-18 14:38:24

标签: java android image watermark

我需要在裁剪后对照片应用水印,但出现了问题。我的应用程序从相机拍摄照片,尝试裁剪它然后应用水印。 这是我从相机拍摄后保存照片的方式(未裁剪)。

Uri uriSavedImage = null;
if (front) {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date date = new Date();
    directory_path = date.toString();
    directory = new File(Environment.getExternalStorageDirectory(), directory_path);
    if (!directory.exists()) {
        directory.mkdirs();
    }
    uriSavedImage = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                        + "/"+ directory_path + "/front.jpg"));
    OutputStream imageFileOS;

    try {
        imageFileOS = getContentResolver().openOutputStream(    uriSavedImage);

        imageFileOS.write(current_pics);

        imageFileOS.flush();

        imageFileOS.close();
        runCropImage(uriSavedImage);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这是尝试裁剪图像:

private void runCropImage(Uri pic_uri) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(pic_uri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    cropIntent.putExtra("outputX", 4800);
    cropIntent.putExtra("outputY", 4800);
    cropIntent.putExtra("return-data", true);
    startActivityForResult(cropIntent, 2);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 2) {
        if (data != null) {
            String cropImagePath = null;
            Bundle extras = data.getExtras();
            Bitmap bitmap = (Bitmap) extras.get("data");
            cropImagePath = Environment.getExternalStorageDirectory()
                        + "/" + directory_path + "/front.jpg";
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(cropImagePath);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.flush();
                        out.close();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
                watermark(Uri.fromFile(new File(cropImagePath)), bitmap);
            }

        } else {
            // TODO do nothing
        }
    }
}

这是我应用水印的方式:

public void watermark(Uri pic_uri, Bitmap bitmap) {

    Bitmap src = BitmapFactory.decodeFile(pic_uri.getPath());

    Bitmap result = Bitmap.createBitmap(1200, 1200, src.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);

    Bitmap waterMark = BitmapFactory.decodeResource(getResources(),
            R.drawable.watermark);

    canvas.drawBitmap(waterMark, canvas.getWidth() - waterMark.getWidth(),
            canvas.getHeight() - waterMark.getHeight(), null);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(pic_uri.getPath());
        result.compress(Bitmap.CompressFormat.JPEG, 80, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Throwable ignore) {
        }
    }
}

问题是水印是否正确应用,但图片会缩放! 所以我有一个新的图像,图片开头的图片(左上角)和正确位置的水印(左下角),然而图片的其余部分是黑色。 那有什么不对? 源裁剪图像具有正确的尺寸,1200x1200(使用文件管理器验证),水印图片也是如此。

我还试图在"位图"上验证尺寸。对象内部的水印功能,但我得到了300x300。

我该如何解决这个问题?

0 个答案:

没有答案