您的位置:首页 > 移动开发 > Android开发

Android调用系统相机拍照,并且模仿实现水印相机简单功能

2013-08-28 18:03 1186 查看
话说最近比较流行水印相机,动不动空间就会用水印相机拍水印照片,本人也比较喜欢,正好今天下午有点时间,就稍微模拟的实现了一下简单功能,不喜勿喷哦~作为学习交流的。
我这边的实现的步骤是通过代码调用系统相机,然后获取拍下来的图片进行水印处理,可以加入水印的图片或者水印文字都行,最后把图片展示和保存在sdcard卡中。(看下效果图:)因为直接用的模拟器,所以相机拍出来的图片直接是系统,比较丑,自己的手机的系统相机被我删掉了。。晕



(一)1:使用代码调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                 startActivityForResult(intent, 1);
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进行了

/** 	 * 进行添加水印图片和文字 	 *  	 * @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); 	}
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(); 				} 			} 		}
有兴趣的朋友可以一起私信我,一起交流学习,共同进步;
代码连载链接地址:http://download.csdn.net/detail/jiangqq781931404/6027909
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息