1

I have some question about water mark within android code!

Following code showed my idea about WaterMark! However,It does not work normally.

e.g. only the image end with .png can be watered mark

Is there a scheme about water mark(.jpeg, .jpg, .wbmp, .bmp, .png or others)

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

Thanks !

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
caopeng
  • 914
  • 13
  • 23

2 Answers2

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.

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • Thanks your answer I do not think your code and my code are different! – caopeng Feb 28 '13 at 09:06
  • Then it's weird that you are unable to get it working with jpg. – Royston Pinto Feb 28 '13 at 09:08
  • I'm so sorry! In fact the code can be run success! The reason of water mark can't be showed ` cv.drawBitmap(waterMark, w - ww, h - wh, null); ` Change to ` cv.drawBitmap(waterMark, w/2, h/2, null); ` The water mark can be marked in the center of album's picture! However,The other question like this http://stackoverflow.com/questions/15135616/how-can-get-gallery2s-album-width-and-height – caopeng Feb 28 '13 at 12:34
0

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

Measure height of multiline text

To center text vertically we need to know text height. Instantiate StaticLayout with text width according to your needs, for us it is simple the width of Bitmap/Canvas minus 16dp padding. The getHeight() then returns height of text. Positioning text on Canvas

There are four simple steps to position text on 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.
opalfire
  • 93
  • 1
  • 9