Как программно добавить водяной знак за пределы изображения? - PullRequest
0 голосов
/ 26 мая 2019

Мне нужен водяной знак под изображением, а не на изображении, когда пользователь сохраняет изображение из приложения, как показано на рисунке. Я могу добавить водяной знак на изображении, но не под изображением, используя следующий код.

 public static Bitmap addWatermark(Resources imageView, Bitmap originalImage) {

    int imageWidth, imageHeight;
    Canvas canvas;
    Paint paint;
    Bitmap resultImage, watermarkImage;
    Matrix matrix;
    float scale;
    RectF rectF;
    imageWidth = originalImage.getWidth();
    imageHeight = originalImage.getHeight();

    // Create the new resultImage
    resultImage = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);

    paint.setColor(Color.parseColor("#f00"));

    // Copy the original resultImage into the new one
    canvas = new Canvas(resultImage);
    canvas.drawBitmap(originalImage, 0, 0, paint);

    // Load the watermarkImage
    watermarkImage = BitmapFactory.decodeResource(imageView, R.mipmap.ic_launcher_foreground);

    // Scale the watermarkImage to be approximately 40% of the originalImage image height
    scale = (float) (((float) imageHeight * 0.20) / (float) watermarkImage.getHeight());

    // Create the matrix
    matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Determine the post-scaled size of the watermarkImage
    rectF = new RectF(0, 0, watermarkImage.getWidth(), watermarkImage.getHeight());
    matrix.mapRect(rectF);

    // Move the watermarkImage to the bottom right corner
    matrix.postTranslate(imageWidth - rectF.width(), imageHeight - rectF.height());

    paint.setAlpha(50);

    // Draw the watermarkImage
    canvas.drawBitmap(watermarkImage, matrix, paint);

    // Free up the resultImage memory
    watermarkImage.recycle();

    return resultImage;
}

enter image description here

...