Java 在照片前面添加水印';在社交媒体上分享
我已经成功地将水印添加到用户在我的android应用程序上拍摄的相机图像预览中,但是当它被发送到Instagram或Tumblr时,水印并不存在。 我相信这是因为它正在共享本地存储的图像,而与预览无关 我认为我需要修改相机的“拍照”代码,这样当它拍摄照片时,它会将其转换为位图,将其添加到带有水印的画布上,然后保存,但我不确定如何执行此操作 我相信这就是共享文件的来源Java 在照片前面添加水印';在社交媒体上分享,java,android,android-camera,watermark,Java,Android,Android Camera,Watermark,我已经成功地将水印添加到用户在我的android应用程序上拍摄的相机图像预览中,但是当它被发送到Instagram或Tumblr时,水印并不存在。 我相信这是因为它正在共享本地存储的图像,而与预览无关 我认为我需要修改相机的“拍照”代码,这样当它拍摄照片时,它会将其转换为位图,将其添加到带有水印的画布上,然后保存,但我不确定如何执行此操作 我相信这就是共享文件的来源 final File fileToUpload = new File(StorageUtils.getStoragePath(Sh
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);
}
});
}
或者我可能太离谱了。任何帮助或指向正确方向都将不胜感激!谢谢大家! 添加到我的评论中,“你走对了方向。在获得图片后,解码它,为它创建一个新画布,在画布上绘制水印,然后保存该图像。你几乎只需重复翻转图像的代码,在保存新图像之前在画布上绘制。” 我厌倦了,为你做了这件事:
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()方法替换为该方法,它将完成您所需的所有操作。您走上了正确的道路。获得图片后,对其进行解码,为其创建新画布,在画布上绘制水印,然后保存该图像。在保存新图像之前,您只需重复翻转图像的代码,只需在画布上绘制即可。谢谢!!它似乎修复了我自己试图解决的所有有趣错误,但当我现在拍照时,我得到了一个java.lang.RuntimeException:takePicture失败于:camera.takePicture(null,null,null,new camera.PictureCallback(){我会尝试解决这个问题,但是如果你知道发生了什么,那也没关系!不客气!嗯,你的logcat应该指向异常发生的那一行,是不是?或者这个问题可能与调用摄影机有关?我注释掉了logcat所在的这两行如果(!drawWatermark(adjustedBitmap))返回,再次感谢您的时间;如果(!saveImage(imageFile,adjustedBitmap))返回,您得到的实际错误是什么?此外,需要配置drawWatermark方法,以便按照您在相机预览中的方式实际绘制水印