如何在Android中添加照片水印

作为一名经验丰富的开发者,我很乐意教会你如何在Android应用中实现照片水印功能。下面是整个流程的概述,我将逐步介绍每个步骤需要做什么,包括所需的代码和注释。

流程图:

flowchart TD
    A(开始)
    B(加载照片)
    C(生成水印)
    D(添加水印)
    E(保存照片)
    F(完成)
    A --> B
    B --> C
    C --> D
    D --> E
    E --> F

类图:

classDiagram
    class Photo {
        -Bitmap image
        -String path
        +Bitmap getImage()
        +void setImage(Bitmap image)
        +String getPath()
        +void setPath(String path)
    }
  
    class Watermark {
        -String text
        -int color
        -int textSize
        -int x
        -int y
        +String getText()
        +void setText(String text)
        +int getColor()
        +void setColor(int color)
        +int getTextSize()
        +void setTextSize(int textSize)
        +int getX()
        +void setX(int x)
        +int getY()
        +void setY(int y)
    }
  
    class WatermarkUtils {
        +Bitmap addWatermark(Bitmap image, Watermark watermark)
    }
  
    Photo --* Watermark
    WatermarkUtils --> Photo
    WatermarkUtils --> Watermark

步骤1:加载照片 首先,你需要加载要添加水印的照片。你可以使用Android的相机或者从相册中选择照片。你需要在你的Activity或Fragment中添加以下代码:

private static final int REQUEST_IMAGE = 100;

// 打开相册
private void openGallery() {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, REQUEST_IMAGE);
}

// 处理返回的照片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE && resultCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
        String imagePath = getPathFromUri(selectedImage);
        Photo photo = new Photo();
        photo.setPath(imagePath);
        // 继续下一步
    }
}

// 从Uri获取照片路径
private String getPathFromUri(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String imagePath = cursor.getString(columnIndex);
    cursor.close();
    return imagePath;
}

步骤2:生成水印 接下来,你需要生成水印。水印可以包含文本、颜色、字体大小和位置等属性。你可以在你的Activity或Fragment中添加以下代码:

private Bitmap generateWatermark(String text) {
    Watermark watermark = new Watermark();
    watermark.setText(text);
    watermark.setColor(Color.RED);
    watermark.setTextSize(30);
    watermark.setX(20);
    watermark.setY(20);
    WatermarkUtils watermarkUtils = new WatermarkUtils();
    return watermarkUtils.addWatermark(photo.getImage(), watermark);
}

步骤3:添加水印 现在,你需要将生成的水印添加到照片上。你可以在你的Activity或Fragment中添加以下代码:

private void addWatermarkToPhoto(Bitmap watermarkImage) {
    Bitmap photoImage = photo.getImage();
    Bitmap watermarkedImage = Bitmap.createBitmap(photoImage.getWidth(), photoImage.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(watermarkedImage);
    canvas.drawBitmap(photoImage, 0, 0, null);
    canvas.drawBitmap(watermarkImage, watermark.getX(), watermark.getY(), null);
    photo.setImage(watermarkedImage);
}

步骤4:保存照片 最后,你需要保存添加水印后的照片。你可以在你的Activity或Fragment中添加以下代码:

private void saveWatermarkedPhoto() {
    Bitmap watermarkedImage = photo.getImage();
    File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "WatermarkedPhoto.jpg");
    try {
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        watermarkedImage.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
        Toast.makeText(this, "照片保存成功", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast