国庆刚放假完好像上班 不怎么做得下去,想了下整理下放假前做的东西
在界面中按设定好的按钮启动系统默认相机
/** * 启动系统相机 * * @param view */ public void takePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CASE_CAMERA); }
拍照后的回调函数
/** * 拍照后的回调函数 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { try { if (resultCode == Activity.RESULT_OK) { if (requestCode == CASE_CAMERA) { Bundle extras = data.getExtras(); // 获取图片信息 Bitmap bitmap = (Bitmap) extras.get("data"); // 保存图片 String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/tmp/"; //// //图片加水印 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time = sdf.format(new Date(System.currentTimeMillis())); Bitmap mWater = BitmapFactory.decodeResource(getResources(), R.drawable.szglogw); Bitmap img = WaterUtils.createBitmap(bitmap, mWater, time); // saveImage(bitmap, imageFilePath); saveImage(img, imageFilePath); } doPhoto(requestCode, data); } } catch (Exception e) { e.printStackTrace(); } }
加水印的工具类:
package com.szg.utils; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.graphics.Bitmap.Config; import android.graphics.Paint.Align; import android.util.Log; /** * 进行添加为照片添加水印图片和文字 帮助类 */ public class WaterUtils { /** * 进行添加水印图片和文字 * * @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; } }
加水印的方法网上找到的