Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 裁剪图像及其水印_Java_Android_Image_Watermark - Fatal编程技术网

Java 裁剪图像及其水印

Java 裁剪图像及其水印,java,android,image,watermark,Java,Android,Image,Watermark,我需要在图片被裁剪后对其应用水印,但出现了一些问题。我的应用程序从相机上拍摄一张照片,尝试裁剪,然后应用水印。 这就是我在从相机拍摄照片后保存照片的方式 Uri uriSavedImage = null; if (front) { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date date = new Date(); directory_path = date.toString();

我需要在图片被裁剪后对其应用水印,但出现了一些问题。我的应用程序从相机上拍摄一张照片,尝试裁剪,然后应用水印。 这就是我在从相机拍摄照片后保存照片的方式

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的设置密度。链接: