2014-10-31 25 views
1

我已經成功地將水印添加到了用戶在我的android應用上採用的相機圖像的預覽,但是當它發送到Instagram或Tumblr時,水印不是'那裏。 我相信這是因爲它是從本地存儲共享圖像,並且與預覽無關。在社交媒體上共享照片之前爲其添加水印

我想我需要修改相機的「拍照」代碼,以便當它將照片轉換爲位圖時,將其添加到帶有水印的畫布上,然後保存它,我不知道該怎麼做。

相信這是被共享

final File fileToUpload = new File(StorageUtils.getStoragePath(ShareActivity.this), StorageUtils.DEFAULT_IMAGE); 

下面是照相機取圖象代碼的文件的源。

protected void takePicture() { 
    if (cameraPreview == null) return; 
    Camera camera = cameraPreview.getCamera(); 
    if (camera == null) return; 

    camera.takePicture(null, null, null, new Camera.PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      if (data == null || data.length == 0) return; 

      File imageFile = new File(StorageUtils.getStoragePath(CameraActivity.this), StorageUtils.DEFAULT_IMAGE); 
      File parentDir = imageFile.getParentFile(); 

      if (!parentDir.exists()) { 
       if (!parentDir.mkdirs()) { 

        Log.d(TAG, "Failed to create directory: " + parentDir.getAbsolutePath()); 
        return; 
       } 
      } 

      try { 
       FileOutputStream fos = new FileOutputStream(imageFile); 
       fos.write(data); 
       fos.close(); 
      } catch (IOException e) { 

       Log.d(TAG, "Failed to save file: " + imageFile.getAbsolutePath()); 
       e.printStackTrace(); 
       return; 
      } 

      //workaround for bug with facing camera introduced (intentionally?) in 4.0+ 
      if (isCameraFacingFront && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
       Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 
       Matrix matrix = new Matrix(); 
       //flip image vertically 
       matrix.setRotate(180); 
       matrix.postScale(-1, 1); 
       Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); 
       bitmap.recycle(); 
       try { 
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(imageFile)); 
        rotatedBitmap.recycle(); 
       } catch (FileNotFoundException e) { 
        Log.d(TAG, "Failed to rotate and save bitmap: " + imageFile.getAbsolutePath()); 
        e.printStackTrace(); 
        return; 
       } 
      } 

      Intent intent = new Intent(CameraActivity.this, ShareActivity.class); 
      intent.putExtra(ShareActivity.PARAM_IMAGE_FILE, imageFile.getAbsolutePath()); 
      if (business != null) 
       intent.putExtra(ShareActivity.PARAM_BUSINESS, business); 
      startActivity(intent); 
     } 
    }); 
} 

或者我可能會離開基地。任何幫助或指向正確的方向非常感謝!謝謝!

+1

你在正確的軌道上。獲取圖片後,對其進行解碼,爲其創建一個新的Canvas,在畫布上繪製水印並保存該圖像。你幾乎要重複翻轉圖像的代碼,只需在保存新圖像之前在畫布上繪製即可。 – Guardanis 2014-10-31 18:40:23

回答

1

添加到我的評論中,「你在正確的軌道上,當你得到圖片後,解碼它,爲它創建一個新的畫布,在畫布上繪製水印,並保存該圖像。 「再非常簡單,只是要正確保存新圖像之前重複代碼翻轉圖像,只需在畫布上繪製「......

我無聊,這樣做是爲了你:

protected void takePicture() { 
    if (cameraPreview == null) return; 
    Camera camera = cameraPreview.getCamera(); 
    if (camera == null) return; 

    camera.takePicture(null, null, null, new Camera.PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      File imageFile = new File(StorageUtils.getStoragePath(CameraActivity.this), StorageUtils.DEFAULT_IMAGE); 
      File parentDir = imageFile.getParentFile(); 
      if(!createImageFromCamera(data, imageFile, parentDir) return; 

      //workaround for bug with facing camera introduced (intentionally?) in 4.0+ 
      boolean requiresImageFlip = isCameraFacingFront && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; 

      Bitmap adjustedBitmap = getBitmap(imageFile, requiresImageFlip); 
      if(!drawWatermark(adjustedBitmap)) return; 
      if(!saveImage(imageFile, adjustedBitmap)) return; 

      Intent intent = new Intent(CameraActivity.this, ShareActivity.class); 
      intent.putExtra(ShareActivity.PARAM_IMAGE_FILE, imageFile.getAbsolutePath()); 
      if(business != null) intent.putExtra(ShareActivity.PARAM_BUSINESS, business); 
      startActivity(intent); 
     } 
    }); 
} 

private Bitmap getBitmap(File imageFile, boolean flipVertically){ 
    Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 
    Matrix matrix = new Matrix(); 

    if(flipVertically){ 
     matrix.setRotate(180); 
     matrix.postScale(-1, 1); 
    } 

    Bitmap adjustedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); 
    bitmap.recycle(); 

    return adjustedBitmap; 
} 

private boolean saveImage(File imageFile, Bitmap bitmap){ 
    try { 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(imageFile)); 
     bitmap.recycle(); 
     return true; 
    } 
    catch (FileNotFoundException e) { 
     Log.d(TAG, "Failed to rotate and save bitmap: " + imageFile.getAbsolutePath()); 
     e.printStackTrace(); 
     return false; 
    } 
} 

private boolean drawWatermark(Bitmap bitmap){ 
    try{ 
     Canvas canvas = new Canvas(bitmap); 
     canvas.drawBitmap(watermarkBitmap); // However you're drawing the watermark on the canvas 
     return true; 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
     return false; 
    } 
} 

private boolean createImageFromCamera(byte[] data, File imageFile, File parentDir){ 
    if (data == null || data.length == 0) return false; 

    if (!parentDir.exists()) { 
     if (!parentDir.mkdirs()) { 
      Log.d(TAG, "Failed to create directory: " + parentDir.getAbsolutePath()); 
      return false;  
     }   
    } 

    try { 
     FileOutputStream fos = new FileOutputStream(imageFile); 
     fos.write(data); 
     fos.close(); 
    } 
    catch (IOException e) { 
     Log.d(TAG, "Failed to save file: " + imageFile.getAbsolutePath()); 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 

用你的整個takePicture()方法替換它,它應該做你想要的一切。

+0

謝謝!它似乎修復了我試圖自行解決這個問題的所有有趣錯誤,但是現在當我拍攝圖片時,我得到一個java.lang.RuntimeException:takePicture failed at:camera.takePicture(null,null,null ,新的Camera.PictureCallback(){我會試着解決這個問題,但如果你知道你的頭頂上發生了什麼也沒關係! – Cassandra 2014-10-31 19:45:04

+0

非常歡迎!嗯,你的logcat應該指向行在哪裏發生了異常,或者不是?或者問題可能與相機的調用有關? – Guardanis 2014-10-31 20:27:05

+0

我注意到logcat指出的這兩行,它不會崩潰?再次感謝您的支持(!saveWatermark(adjustedBitmap))return; if(!saveImage(imageFile,adjustedBitmap))return; – Cassandra 2014-10-31 20:58:28

相關問題