2014-12-25 6 views
3

안드로이드 앱에서 이미지에 워터 마크를 추가하고 싶습니다. 내 응용 프로그램 사용자가 이미지를 선택하고 편집 텍스트에 텍스트를 쓸 수 있습니다. 버튼을 누르면 선택한 이미지에 워터 마크 텍스트가 필요합니다. 아래 코드를 시도했지만 작동하지 않습니다.안드로이드 이미지에 워터 마크 추가

public static void mark(String watermark,String filePath) { 
     try{ 
       Bitmap bmp = BitmapFactory.decodeFile(filePath); 
       int w = bmp.getWidth(); 
       int h = bmp.getHeight(); 
       Bitmap result = Bitmap.createBitmap(100, 100, bmp.getConfig()); 
       Canvas canvas = new Canvas(result); 
       canvas.drawBitmap(bmp, 0, 0, null); 
       Paint paint = new Paint(); 
       paint.setColor(Color.WHITE); 
       paint.setAlpha(40); 
       paint.setTextSize(20); 
       paint.setAntiAlias(true); 
       canvas.drawText(watermark,w/2, h/2+20, paint); 
       File nFile = new File(getDevicePath(mContext)+"output1.jpg"); 
       nFile.createNewFile(); 
       FileOutputStream fOut = new FileOutputStream(nFile); 
       bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
       fOut.flush(); 
       fOut.close(); 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 

위의 문제를 해결하는 방법을 알려주십시오.

미리 감사드립니다.

+0

문제가 있습니까? –

+0

어떤면에서 "작동하지 않습니까?" –

+0

http://shaikhhamadali.blogspot.in/2013/08/water-mark-image-in-imageview-android.html 나는 어떤 솔루션이라도 가지고 있다고 생각한다. – ckpatel

답변

0

내 fault.i는 내 자신의 문제에 대한 해결책을 찾았습니다. 답변 주셔서 감사합니다. 잘못된 비트 맵을 사용하고 있습니다. 라인
- 내가 잘못 비트 맵을 사용하고 있었다

bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 

이 난의 working.instead되지 않은 이유, 그건이

result.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 

를 사용해야 라인 위의 변경 후 마법 같은 작품이다.

다른 도움이 되길 바랍니다.

1

문제 1 : "워터 마크가 삽입 된 이미지"가 원본 이미지와 같은 크기가 아니길 원하십니까? 그래서, 대신 하드 코딩 값 :

  Bitmap result = Bitmap.createBitmap(w, h, bmp.getConfig()); 

문제 2 : 그것은 당신이 쓰는 할 result 비트 맵 아닌가? 그래서, 대신 :

  result.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 

문제 3 : 사진이 원하는대로 표시되지 않더라도 원하는 위치에 파일이 있습니까?

  File nFile = new File(getDevicePath(mContext)+ File.separator + "output1.jpg"); 
+0

답변 해 주셔서 감사합니다. + 1 답변. – Ravi

1

이 코드를 메서드에 정의하고이를 구현하기위한 동적 텍스트 뷰를 생성하십시오. 이것은 나를 위해 잘 작동하고 imgPreviewRotate 내 이미지 뷰입니다 그래서 imageview 변경하고 체크 아웃하십시오.

TextView txtWatermark = new TextView(MainActivity.this); 
txtWatermark.setGravity(Gravity.RIGHT | Gravity.BOTTOM); 
txtWatermark.setText(getResources().getString("Dummy text")); 
txtWatermark.setTextSize(5.0f); 
txtWatermark.setPadding(10, 0, 30, 0); 
txtWatermark.setTypeface(FontStyle.OswaldRegular(PreviewActivity.this)); 
txtWatermark.setTextColor(Color.parseColor("#FF0000")); 
rlPreview.addView(imgPreviewRotate); 
rlPreview.addView(txtWatermark); 
txtWatermark.setVisibility(View.INVISIBLE);