Android 調(diào)用系統(tǒng)相機拍照后 添加文字水印

昨天需要添加照片水印的功能,google后找到了博客大v 水寒的文章 對我?guī)椭艽?http://blog.csdn.net/dawanganban/article/details/51148070
大v的文章 很有深度 非常有用
添加水印有了大v的工具類不成問題 但是在獲取拍照后返回的圖片時,由于使用的是拍照后的縮略圖,導致處理后圖片模糊不清;所以想到了先保存原圖,再通過原圖添加水印(以下方式,獲取的是稍大的縮略圖,比系統(tǒng)默認的清晰);

1.先設置拍照后的文件路徑
   String photoName = UtilTools.getFileName(1);
2.通過Intent調(diào)用系統(tǒng)相機
   Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(photoName)));
                intent1.putExtra("return-data", true);// 不加有時會返回data為空
                startActivityForResult(intent1, 1);
3.處理返回值
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
         switch (requestCode) {
            case HttpServer.REQUEST_CAPTURE_IMAGE:
                UtilTools.makePhoto(getBaseContext(),photoName);
               Bitmap bitmap = UtilTools.convertToBitmap(photoName, 100, 100);
    ImageView imgview = new ImageView(MainActivity.this);
    imgview.setImageBitmap(bitmap);
                break;
}
  super.onActivityResult(requestCode, resultCode, data);
  }

用到的工具類

1.getFileName: 設置文件名字
 public static String getFileName(int requestCode) {
  SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);
    Date curDate = new Date(System.currentTimeMillis());
    String  name = formater.format(curDate);
    if (requestCode == 1)
        name = SdcardPath.sdPicPath() + name + ".jpg";
    else if (requestCode == 2)
        name = SdcardPath.sdVedioPath() + name + ".mp4";
    return name;
}
2.sdPicPath 設置保存路徑
   /**
     * 判斷是否存在照片存儲的路徑,不存在則創(chuàng)建
     */
public static String sdPicPath() {
    File sd = Environment.getExternalStorageDirectory();
    String path = sd.getPath() + "/DIST_File/Picture";
    File dir = new File(path);
    if (!dir.exists())
        dir.mkdirs();
    return  path + "/";
}
3.UtilTools.makePhoto 添加水印并保存文件
 //添加圖片水印 并保存
public static void makePhoto(Context context, String photoName) {
    FileOutputStream b = null;
    Bitmap bitmap = UtilTools.convertToBitmap(photoName, 500, 500);   //獲取原圖的縮略圖500*500
    SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.CHINA);
    Date curDate = new Date(System.currentTimeMillis());
    String date = formater.format(curDate);   //獲取當前的日期事件信息
    if (bitmap != null) {
        Bitmap bitmap1 = ImageUtil.drawTextToLeftBottom(context, bitmap, date, 16, Color.RED, 45, 30);  //添加第一個時間水印
        Bitmap bitmap2 = ImageUtil.drawTextToLeftBottom(context, bitmap1, HndistConstant.X_Yzuobiao, 16, Color.RED, 45, 20); //添加坐標水印
        try {
            b = new FileOutputStream(photoName);
            bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把圖片數(shù)據(jù)寫入指定的文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (b != null) {
                    b.flush();   //刷新輸出流
                    b.close();   //關閉輸出流
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
4.UtilTools.convertToBitmap 獲取圖片的壓縮文件
 /**
   * 壓縮圖片為位圖
   */
public static Bitmap convertToBitmap(String path, int w, int h) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    // 設置為ture只獲取圖片大小
    opts.inJustDecodeBounds = true;
    opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
    // 返回為空
    BitmapFactory.decodeFile(path, opts);
    int width = opts.outWidth;
    int height = opts.outHeight;
    float scaleWidth = 0.f, scaleHeight = 0.f;
    if (width > w || height > h) {
        // 縮放
        scaleWidth = ((float) width) / w;
        scaleHeight = ((float) height) / h;
    }
    opts.inJustDecodeBounds = false;
    float scale = Math.max(scaleWidth, scaleHeight);
    opts.inSampleSize = (int) scale;
    WeakReference<Bitmap> weak = new WeakReference<>(
            BitmapFactory.decodeFile(path, opts));
    return Bitmap.createBitmap(weak.get());
}

經(jīng)過一番折騰 終于獲取了 想要的圖片 美中不足的是水印有點模糊 整體效果還好

IMG_20161101_100806_HDR.jpg

昨天更新了demo

源代碼地址→

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容