话说最近比较流行水印相机,动不动空间就会用水印相机拍水印照片,本人也比较喜欢,正好今天下午有点时间,就稍微模拟的实现了一下简单功能,不喜勿喷哦~作为学习交流的。
我这边的实现的步骤是通过代码调用系统相机,然后获取拍下来的图片进行水印处理,可以加入水印的图片或者水印文字都行,最后把图片展示和保存在sdcard卡中。(看下效果图:)因为直接用的模拟器,所以相机拍出来的图片直接是系统,比较丑,自己的手机的系统相机被我删掉了。。晕
(一)1:使用代码调用系统相机
- <span style="font-size:18px;">Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(intent, 1);</span>
具体方法为:Bundle bundle = data.getExtras();
//获取拍照返回的图片
bitmap= (Bitmap) bundle.get("data");
(二)1:对图片进行加水印出来,方法比较简单,直接用Canvas进行drawBitmap还有drawText进行了
- /**
- * 进行添加水印图片和文字
- *
- * @param src
- * @param waterMak
- * @return
- */
- public static Bitmap createBitmap(Bitmap src, Bitmap waterMak, String title) {
- if (src == null) {
- return src;
- }
- // 获取原始图片与水印图片的宽与高
- int w = src.getWidth();
- int h = src.getHeight();
- int ww = waterMak.getWidth();
- int wh = waterMak.getHeight();
- Log.i("jiangqq", "w = " + w + ",h = " + h + ",ww = " + ww + ",wh = "
- + wh);
- Bitmap newBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
- Canvas mCanvas = new Canvas(newBitmap);
- // 往位图中开始画入src原始图片
- mCanvas.drawBitmap(src, 0, 0, null);
- // 在src的右下角添加水印
- Paint paint = new Paint();
- //paint.setAlpha(100);
- mCanvas.drawBitmap(waterMak, w - ww - 5, h - wh - 5, paint);
- // 开始加入文字
- if (null != title) {
- Paint textPaint = new Paint();
- textPaint.setColor(Color.RED);
- textPaint.setTextSize(16);
- String familyName = "宋体";
- Typeface typeface = Typeface.create(familyName,
- Typeface.BOLD_ITALIC);
- textPaint.setTypeface(typeface);
- textPaint.setTextAlign(Align.CENTER);
- mCanvas.drawText(title, w / 2, 25, textPaint);
- }
- mCanvas.save(Canvas.ALL_SAVE_FLAG);
- mCanvas.restore();
- return newBitmap;
- }
- if (img != null) {
- water_img.setImageBitmap(img);
- //把水印图片也保存到sdcard中
- FileUtils.saveFile(img, sdf.format(new Date(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);
- }
- /**
- * 把图片村保存在相应的文件当中
- * @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();
- }
- }
- }