2012-11-07 131 views
4

我的代碼中添加圖像水印這樣添加水印圖像中的android

public static Bitmap mark(Bitmap src, String watermark, Point location, Color color, int alpha, int size, boolean underline) { 
      int w = src.getWidth(); 
      int h = src.getHeight(); 
      Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); 

      Canvas canvas = new Canvas(result); 
      canvas.drawBitmap(src, 0, 0, null); 

      Paint paint = new Paint(); 
      paint.setColor(color.RED); 
      paint.setAlpha(alpha); 
      paint.setTextSize(size); 
      paint.setAntiAlias(true); 
      paint.setUnderlineText(underline); 
      canvas.drawText(watermark, location.x, location.y, paint); 

      return result; 
     } 

,我調用該函數與此代碼

mark(bitmap, "watermark", b, null, c, 100, false); 
      imgshoot.setImageBitmap(bitmap); 

,但沒有發生,u能幫助我嗎?感謝

回答

8

它解決了,我只是改變了很少的代碼,並感謝烏拉圭回合的建議Doomsknight :)

public static Bitmap mark(Bitmap src, String watermark) { 
    int w = src.getWidth(); 
    int h = src.getHeight(); 
    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); 
    Canvas canvas = new Canvas(result); 
    canvas.drawBitmap(src, 0, 0, null); 
    Paint paint = new Paint(); 
    paint.setColor(Color.RED); 
    paint.setTextSize(18); 
    paint.setAntiAlias(true); 
    paint.setUnderlineText(true); 
    canvas.drawText(watermark, 20, 25, paint); 

    return result; 
} 

,我用這個功能

bitmap = mark(bitmap, "Hallo"); 
imgshoot.setImageBitmap(bitmap); 
2

你是不是你的分配返回的位圖result任何東西。用舊的位圖替換舊的位圖。

bitmap = mark(bitmap, "watermark", b, null, c, 100, false); 
imgshoot.setImageBitmap(bitmap); 

編輯:

按照意見,您仍然有問題:嘗試硬編碼了一些參數,可以進行測試。看你的params是否有問題。

 Paint paint = new Paint(); 
     paint.setColor(color.RED); 
     //paint.setAlpha(alpha); 
     paint.setTextSize(20); //size 
     //paint.setAntiAlias(true); 
     paint.setUnderlineText(underline); 
     canvas.drawText(watermark, 10, 10, paint); //location.x, location.y 
+0

我的形象打電話仍然沒有水印:( –

+0

@yaLya看到我的編輯。這是確定你的代碼必須包括第一位,但你似乎有兩個問題。像我一樣甩掉參數。看看會發生什麼。如果這樣做,檢查你的參數,並看看哪一個是錯誤的。 :) – Doomsknight

+0

對不起,但沒有發生太多,meaby我的代碼水印圖像是錯誤的:( –

0
private Bitmap addWaterMark(Bitmap src) 
{ 
    int w = src.getWidth(); 
    int h = src.getHeight(); 
    Bitmap result = Bitmap.createBitmap(w,h,src.getConfig()); 
    Canvas canvas = new Canvas(result); 
    canvas.drawBitmap(src,0,0, null); 
    Bitmap waterMark = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_laucher); 
    canvas.drawBitmap(waterMark,0,0,null); 

    return result; 
}