2014-11-23 69 views
0

我使用手機拍照相機應用程序來捕獲圖像。 實際上,這是來自android開發人員網站的示例代碼,介紹如何簡單地拍照,以及適合我應用需求的一些調整。Android - 如何在保存圖像之前對圖像進行水印處理

public void dispatchTakePictureIntent(int actionCode) { 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     File f = null; 
     try { 
      f = setUpPhotoFile(); 
      mCurrentPhotoPath = f.getAbsolutePath(); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      f = null; 
      mCurrentPhotoPath = null; 
     } 
    startActivityForResult(takePictureIntent, actionCode); 
} 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mImageBitmap = null; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { 
     mAlbumStorageDirFactory = new FroyoAlbumDirFactory(); 
    } else { 
     mAlbumStorageDirFactory = new BaseAlbumDirFactory(); 
    } 
    dispatchTakePictureIntent(ACTION_TAKE_PHOTO); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (mCurrentPhotoPath != null) { 
      Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE"); 
      File f = new File(mCurrentPhotoPath); 
      Uri contentUri = Uri.fromFile(f); 
      mediaScanIntent.setData(contentUri); 
      this.sendBroadcast(mediaScanIntent); 
      mCurrentPhotoPath = null; 
     } 
     dispatchTakePictureIntent(ACTION_TAKE_PHOTO); 
    } 
    else{ 
    finish(); 
    } 
} 

和水印用字符串的位圖的方法。

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

問題是我無法獲得位圖使用此方法。

非常感謝提前!

+0

創建從相機 – Praveen 2014-11-23 10:46:59

+0

返回我可以將文件轉換F到的位圖,然後用標記方法的數據位圖,但在此之後,是什麼? – Vissarionas 2014-11-23 16:07:21

+0

將其保存爲文件 – Praveen 2014-11-23 18:00:04

回答

0

你傳遞一個文件Uri來保存拍攝的圖片。所以試試這個。我已更改您的onActivityResult方法。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (resultCode == RESULT_OK) { 
    if (mCurrentPhotoPath != null) { 
     Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE"); 
     File f = new File(mCurrentPhotoPath); 
     Uri contentUri = Uri.fromFile(f); 
     Bitmap photo = Media.getBitmap(getContentResolver(), contentUri); 
     photo=mark(photo, yourWatreStringHere); 
     OutputStream fOut = null; 
     fOut = new FileOutputStream(f); 
     photo.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate 
     fOut.flush(); 
     fOut.close(); // do not forget to close the stream 
     mediaScanIntent.setData(contentUri); 
     this.sendBroadcast(mediaScanIntent); 
     mCurrentPhotoPath = null; 
    } 
    dispatchTakePictureIntent(ACTION_TAKE_PHOTO); 
} 
else{ 
finish(); 
} 
} 
+0

男士,感謝您的耐心等待。我試過了,沒有mark()方法。我已經把它放在一個asynctask中,它工作正常,但它旋轉了圖像。我以某種方式得到它。問題必須在mark()方法中。有什麼建議麼? – Vissarionas 2014-11-26 17:14:10

+0

大多數手機相機都是風景,這意味着如果您以縱向拍攝照片,最終的照片將旋轉90度。在這種情況下,相機軟件應該使用照片應該被查看的方向來填充EXIF數據。看看這個問題http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent -gets旋轉的 - 上一些的設備功能於安卓 – Praveen 2014-11-26 19:30:00