我已經成功地將水印添加到了用戶在我的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);
}
});
}
或者我可能會離開基地。任何幫助或指向正確的方向非常感謝!謝謝!
你在正確的軌道上。獲取圖片後,對其進行解碼,爲其創建一個新的Canvas,在畫布上繪製水印並保存該圖像。你幾乎要重複翻轉圖像的代碼,只需在保存新圖像之前在畫布上繪製即可。 – Guardanis 2014-10-31 18:40:23