gpt4 book ai didi

java - 在社交媒体上分享之前为照片添加水印

转载 作者:行者123 更新时间:2023-11-30 02:35:14 25 4
gpt4 key购买 nike

我已经成功地为用户在我的 Android 应用程序上拍摄的相机图像预览添加了水印,但是当它被发送到 Instagram 或 Tumblr 时,水印不存在。我相信这是因为它是从本地存储共享图像,与预览无关。

我想我需要修改相机的“拍照”代码,以便在拍照时将其转换为位图,将其添加到带有水印的 Canvas 中,然后保存,但我不知道该怎么做。

我相信这是共享文件的来源

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 ,在 Canvas 上绘制水印,然后保存该图像。你”我们几乎只是重复翻转图像的代码,只是在保存新图像之前在 Canvas 上绘图。”...

我很无聊,为你做了:

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 - 在社交媒体上分享之前为照片添加水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26681304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com