摘要:Android调用系统相机拍照,并且模仿实现水印相机简单功能
话说最近比较流行水印相机,动不动空间就会用水印相机拍水印照片,本人也比较喜欢,正好今天下午有点时间,就稍微模拟的实现了一下简单功能,不喜勿喷哦~作为学习交流的。
我这边的实现的步骤是通过代码调用系统相机,然后获取拍下来的图片进行水印处理,可以加入水印的图片或者水印文字都行,最后把图片展示和保存在sdcard卡中。(看下效果图:)因为直接用的模拟器,所以相机拍出来的图片直接是系统,比较丑,自己的手机的系统相机被我删掉了。。晕
(一)1:使用代码调用系统相机
|
1
2
|
<span
style="font-size:18px;">Intent
intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1);</span> |
2:使用系统相机拍照确定之后,返回回来,在之前的Activity中重载protected void onActivityResult(int requestCode, int resultCode, Intent data)来获取其中的Bitmap对象.
具体方法为:Bundle bundle = data.getExtras();
//获取拍照返回的图片
bitmap= (Bitmap) bundle.get("data");
(二)1:对图片进行加水印出来,方法比较简单,直接用Canvas进行drawBitmap还有drawText进行了
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/** *
进行添加水印图片和文字 * *
@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; } |
这样这个方法就会返回回去一个已经构造好的加有水印的图片,然后进行保存显示:
|
1
2
3
4
5
6
7
|
if (img
!= null)
{ water_img.setImageBitmap(img); //把水印图片也保存到sdcard中 FileUtils.saveFile(img,
sdf.format(newDate(System.currentTimeMillis()))+"2.jpg"); } }else { Log.i("jiangqq", "拍照失败."); |
(三)其中用的保存文件的工具类为:
1:检测sdcard卡
|
1
2
3
4
5
|
//
判断SD卡是否存在 public static boolean externalMemoryAvailable()
{ return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } |
2:图片保存至文件当中
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/** *
把图片村保存在相应的文件当中 *
@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(); } } } |

被折叠的 条评论
为什么被折叠?



