是否有任何支持Android图像的库可以添加水印?

时间:2017-07-06 04:20:26

标签: android bitmap

我尝试了很长时间并且无法改善,在不同的手机上:
1.我想在sourceBitmap中添加图片和文字 2.希望能够调整位图和字位。

2 个答案:

答案 0 :(得分:2)

不使用库我们可以使用画布和绘画概念水印图像

 Point point=new Point();
 point.set(180, 1000);
 Bitmap b=waterMark(BitmapFactory.decodeResource(getResources(), R.drawable.image),"your Text",point,Color.WHITE,90,30,true);
 imageView.setImageBitmap(b);

方法代码

public  Bitmap waterMark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline) {
 //get source image width and height
 int w = src.getWidth();
 int h = src.getHeight();

 Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
 //create canvas object
 Canvas canvas = new Canvas(result);
 //draw bitmap on canvas
 canvas.drawBitmap(src, 0, 0, null);
 //create paint object
 Paint paint = new Paint();
 //apply color
 paint.setColor(color);
 //set transparency
 paint.setAlpha(alpha);
 //set text size
 paint.setTextSize(size);
 paint.setAntiAlias(true);
 //set should be underlined or not
 paint.setUnderlineText(underline);
 //draw text on given location
  canvas.drawText(watermark, location.x, location.y, paint); 
 return result;
}

答案 1 :(得分:0)

此方法将根据位图的大小自动调整水印文本的大小。此外,如果图像是纵向的,它也会在对角线处添加水印,如果图像是横向的,则将水平添加

public static Bitmap waterMark(Bitmap src, String watermark, int color, int alpha, int size) {
    //get source image width and height
    int w = src.getWidth();
    int h = src.getHeight();


    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
    //create canvas object
    Canvas canvas = new Canvas(result);
    //draw bitmap on canvas
    canvas.drawBitmap(src, 0, 0, null);
    //create paint object
    Paint paint = new Paint();
    //apply color
    paint.setColor(color);

    //set transparency
    paint.setAlpha(alpha);
    //set text size
    paint.setTextSize(size);
    paint.setAntiAlias(true);

    Paint stkPaint = new Paint();
    stkPaint.setStyle(Paint.Style.STROKE);
    stkPaint.setStrokeWidth(1);
    stkPaint.setTextSize(size);
    stkPaint.setAlpha(alpha);
    stkPaint.setColor(Color.BLACK);
    stkPaint.setAntiAlias(true);
    //set should be underlined or not

    //draw text on given location
    canvas.save();

    float width = paint.measureText(watermark);
    Logger.e("width of bitmap", "" + w);
    Logger.e("width of text", "" + width);
    if (width > w) {
        int i = 2;
        while (width > w) {
            paint.setTextSize(size - i);
            stkPaint.setTextSize(size - i);
            width = paint.measureText(watermark);
            Logger.e("width of text", "" + width);
            i++;
        }
    } else {
        int i = 2;
        while (width < w) {
            paint.setTextSize(size + i);
            stkPaint.setTextSize(size + i);
            width = paint.measureText(watermark);
            Logger.e("width of text", "" + width);
            i++;
        }
    }

    if (w < h) {
        canvas.rotate(-60, src.getWidth() / 2, src.getHeight() / 2);
    }


    float final_x = (src.getWidth() - width) / 2;
    Point p = new Point();
    p.set((int) final_x, src.getHeight() / 2);

    canvas.drawText(watermark, p.x, p.y, paint);
    canvas.drawText(watermark, p.x, p.y, stkPaint);

    canvas.restore();

    return result;
}