我已经成功地将水印添加到了用户在我的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