Android水印

时间:2013-02-28 08:34:15

标签: android watermark

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

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

e.g。只有带有.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;
    } 

谢谢!

2 个答案:

答案 0 :(得分:4)

这是我用来将水印应用到jpeg的代码,它也适用于你,

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

基本上在此之后你必须使用Bitmap.compress(<arguments>)来获取它的jpg。

不要尝试其他格式。可能有可能你可以从中提取Bitmap,就像我们为jpg和png做的那样。

答案 1 :(得分:0)

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

测量多行文字的高度

要使文本垂直居中,我们需要知道文本的高度。根据您的需要使用文本宽度实例化StaticLayout,对我们来说,这是简单的位图/画布宽度减去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.