1

我对android代码中的水印有一些疑问!

以下代码显示了我对 WaterMark 的想法!但是,它不能正常工作。

例如,只有以 .png 结尾的图像才能被水印

是否有关于水印的方案(.jpeg、.jpg、.wbmp、.bmp、.png 或其他)

   protected static Bitmap getDrmPicture(Context context,String path){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap originMap = BitmapFactory.decodeFile (path,options);
        Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.close);

        InputStream input;
        byte[] b;
        Bitmap waterMark = null;
        try {
            input = context.getResources().openRawResource(R.drawable.lock);
            b = new byte[input.available()];
            input.read(b);
            waterMark =  DecodeUtils.requestDecode(jc, b, null);
        }catch(IOException e){
        }

        int w = originMap.getWidth();
        int h = originMap.getHeight();

        int ww = waterMark.getWidth();
        int wh = waterMark.getHeight();

        Bitmap newb = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888;);
        Canvas cv = new Canvas(newb);
        cv.drawBitmap(originMap, 0, 0, null);
        cv.drawBitmap(waterMark, w - ww, h - wh, null);
        cv.save(Canvas.ALL_SAVE_FLAG);
        cv.restore();

        return newb;
    } 

谢谢 !

4

2 回答 2

4

This is the code I use to apply watermark to a jpeg, it should work for you too,

public Bitmap applyWatermarkColorFilter(Drawable drawable) { 
    Bitmap image = ((BitmapDrawable)drawable).getBitmap();

    Bitmap result = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(image, 0, 0, null);

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

    canvas.drawBitmap(watermark, image.getWidth()/2 - watermark.getWidth()/2, 
            image.getHeight()/2 - watermark.getHeight()/2, 
            null);

    return result;
}

Basically after this u have to use Bitmap.compress(<arguments>) to get a jpg out of it.

Din't try for the other formats. May be it might be possible if you can extract the Bitmap out of them like how we do for jpg and png.

于 2013-02-28T08:51:04.690 回答
0

https://stackoverflow.com/questions/6756975/draw-multi-line-text-to-canvas

测量多行文本的高度

要使文本垂直居中,我们需要知道文本高度。根据您的需要使用文本宽度实例化 StaticLayout,对我们来说,这很简单,即 Bitmap/Canvas 的宽度减去 16dp 填充。getHeight() 然后返回文本的高度。在画布上定位文本

在 Canvas 上放置文本有四个简单的步骤:

Save the current matrix and clip with Canvas.save().
Apply translation to Canvas matrix with Canvas.translate(x,y).
Draw StaticLayout on Canvas with StaticLayout.draw(canvas).
Revert matrix translation with Canvas.restore() method.
于 2018-08-22T11:11:46.913 回答