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

时间:2014-10-31 18:35:02

标签: java android android-camera watermark

我已成功添加水印到用户在我的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 个答案:

答案 0 :(得分: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()方法,它应该做你想要的一切。