话说比来比力流行水印相机,动不动空间就会用水印相机拍水印照片,本人也比力爱好,正好明天下战书有点时分,就稍微摹拟的完成了一下俭朴功用,不喜勿喷哦~作为进修交流的。
我这边的完成的步伐是经由过程代码挪用系统相机,然后取得拍上去的图片中止水印处置,能够插手水印的图片或水印文字都行,最初把图片展示和保存在sdcard卡中。(看下结果图:)由于直接用的摹拟器,所以相机拍出来的图片直接是系统,比力丑,自身的手机的系统相机被我删失落了。。晕
%20 %20 %20 (一)1:应用代码挪用系统相机%20 %20
%20span%20Intent%20intent%20=%20new%20Intent(MediaStore.ACTION_IMAGE_CAPTURE);%20%20startActivityForResult(intent,%201);%20/span%20
2:应用系统相机摄影肯定以后,前往回来,在之前的Activity中重载protected%20void%20onActivityResult(int%20requestCode,%20int%20resultCode,%20Intent%20data)来取得此中的Bitmap对象.
%20细致方式为:Bundle%20bundle%20=%20data.getExtras();
//取得摄影前往的图片
bitmap=%20(Bitmap)%20bundle.get(%20data%20
%20 %20 (二)1:对图片中止加水印出来,方式比力俭朴,直接用Canvas中止drawBitmap还有drawText中止了%20
/**%20%20*%20中止添加水印图片和文字%20%20*%20@param%20src%20%20*%20@param%20waterMak%20%20*%20@return%20%20public%20static%20Bitmap%20createBitmap(Bitmap%20src,%20Bitmap%20waterMak,%20String%20title)%20{%20%20if%20(src%20==%20null)%20{%20%20return%20src;%20%20//%20取得原始图片与水印图片的宽与高%20%20int%20w%20=%20src.getWidth();%20%20int%20h%20=%20src.getHeight();%20%20int%20ww%20=%20waterMak.getWidth();%20%20int%20wh%20=%20waterMak.getHeight();%20%20Log.i("jiangqq",%20"w%20=%20"%20+%20w%20+%20",h%20=%20"%20+%20h%20+%20",ww%20=%20"%20+%20ww%20+%20",wh%20=%20"%20%20+%20wh);%20%20Bitmap%20newBitmap%20=%20Bitmap.createBitmap(w,%20h,%20Config.ARGB_8888);%20%20Canvas%20mCanvas%20=%20new%20Canvas(newBitmap);%20%20//%20往位图中起头画进src原始图片%20%20mCanvas.drawBitmap(src,%200,%200,%20null);%20%20//%20在src的右下角添加水印%20%20Paint%20paint%20=%20new%20Paint();%20%20//paint.setAlpha(100);%20%20mCanvas.drawBitmap(waterMak,%20w%20-%20ww%20-%205,%20h%20-%20wh%20-%205,%20paint);%20%20//%20起头插手文字%20%20if%20(null%20!=%20title)%20{%20%20Paint%20textPaint%20=%20new%20Paint();%20%20textPaint.setColor(Color.RED);%20%20textPaint.setTextSize(16);%20%20String%20familyName%20=%20"宋体";%20%20Typeface%20typeface%20=%20Typeface.create(familyName,%20%20Typeface.BOLD_ITALIC);%20%20textPaint.setTypeface(typeface);%20%20textPaint.setTextAlign(Align.CENTER);%20%20mCanvas.drawText(title,%20w%20/%202,%2025,%20textPaint);%20%20mCanvas.save(Canvas.ALL_SAVE_FLAG);%20%20mCanvas.restore();%20%20return%20newBitmap;%20%20}
多么这个方式就会前往归去一个已机关好的加有水印的图片,然落后行保存显现:%20
if%20(img%20!=%20null)%20{%20%20water_img.setImageBitmap(img);%20%20//把水印图片也保存到sdcard中%20%20FileUtils.saveFile(img,%20sdf.format(new%20Date(System.currentTimeMillis()))+"2.jpg"); }else { Log.i("jiangqq", "摄影掉败.");
(三)此中用的保存文件的东西类为:
1:检测sdcard卡
// 断定SD卡是不是存在 public static boolean externalMemoryAvailable() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); }
2:图片保存至文件傍边
/** * 把图片村保存在响应的文件傍边 * @param pBitmap * @param pPathName public static void saveFile(Bitmap pBitmap,String fileName) File file=new File("/sdcard/pps_image"); if(!file.exists()) file.mkdirs(); String filePathName=file.getAbsolutePath()+"/"+fileName; FileOutputStream fos=null; try { fos=new FileOutputStream(filePathName); pBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); Log.i("jiangqq", "保存图片到sdcard卡胜利."); } catch (Exception e) { e.printStackTrace(); }finally if(fos!=null) try { fos.close(); } catch (IOException e) { e.printStackTrace(); }
有乐趣的伴侣能够一路私信我,一路交流进修,配合行进;