Android实现拍照添加时间水印
时间:2022-11-17 10:14:52|栏目:Android代码|点击: 次
本文实例为大家分享了Android实现拍照添加时间水印的具体代码,供大家参考,具体内容如下
效果如下图 :
1、拍照
// 非空判断 拍照 if (mCamera0 != null){ mCamera0.takePicture(null, null, jpeg0); // 1 front }
2、创建JPEG数据回调
// 创建JPEG图片回调数据对象 public static PictureCallback jpeg0 = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { // 获得图片 Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); //添加时间水印 Bitmap mTimeWatermark = AddTimeWatermark(mBitmap); // 判断文件夹是否存在,传入图片存储路径 CreateFile(StaticVariables.mPicPath); String mPath = StaticVariables.mPicPath + System.currentTimeMillis()+".jpeg"; //临时测试命名 File mFile = new File(mPath); BufferedOutputStream mOutputStream = new BufferedOutputStream(new FileOutputStream(mFile)); // 将图片压缩到流中 mTimeWatermark.compress(Bitmap.CompressFormat.JPEG,100,mOutputStream); //时间水印 mOutputStream.flush(); mOutputStream.close(); // 停止预览 mCamera0.stopPreview(); mCamera0.release(); mCamera0 = Camera.open(4); Parameters parameters = mCamera0.getParameters(); // parameters.setPreviewSize(width, height); mCamera0.setParameters(parameters); mCamera0.setDisplayOrientation(0); mCamera0.setPreviewTexture(mTextureView0.getSurfaceTexture()); // 处理完数据之后预览 mCamera0.startPreview(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } };
3、添加时间水印:AddTimeWatermark(mBitmap);
/** * 添加时间水印 * @param mBitmap * @return mNewBitmap */ private Bitmap AddTimeWatermark(Bitmap mBitmap) { //获取原始图片与水印图片的宽与高 int mBitmapWidth = mBitmap.getWidth(); int mBitmapHeight = mBitmap.getHeight(); //定义底片 大小 将mBitmap填充 Bitmap mNewBitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888); Canvas mCanvas = new Canvas(mNewBitmap); //向位图中开始画入MBitmap原始图片 mCanvas.drawBitmap(mBitmap,0,0,null); //添加文字 Paint mPaint = new Paint(); String mFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EEEE").format(new Date()); //String mFormat = TingUtils.getTime()+"\n"+" 纬度:"+GpsService.latitude+" 经度:"+GpsService.longitude; mPaint.setColor(Color.RED); mPaint.setTextSize(20); //水印的位置坐标 mCanvas.drawText(mFormat, (mBitmapWidth * 1) / 10,(mBitmapHeight*14)/15,mPaint); // mCanvas.save(Canvas.ALL_SAVE_FLAG); mCanvas.save(); mCanvas.restore(); return mNewBitmap; }
4、创建文件夹
// 创建 文件夹 public static void CreateFile(String path) { File destDir = new File(path); if (!destDir.exists()) { Log.i(TAG,"文件夹创建成功"); destDir.mkdirs(); } else { Log.i(TAG,"文件夹已存在"); } }
5、Java 实现添加水印
/** * 设置文字水印 * * @param sourceImg * 源图片路径 * @param targetImg * 保存的图片路径 * @param content * 内容 * @param font * 水印字体大小 * Font font = new Font("微软雅黑", Font.BOLD, 16); * @throws IOException */ public void addWatermark(String sourceImg, String targetImg,String content, Font font) throws IOException { File srcImgFile = new File(sourceImg); Image srcImg = ImageIO.read(srcImgFile); int srcImgWidth = srcImg.getWidth(null); int srcImgHeight = srcImg.getHeight(null); BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight,BufferedImage.TYPE_INT_RGB); Graphics2D g = bufImg.createGraphics(); g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null); g.setColor(Color.BLACK); g.setFont(font); if (content.equals("model")) { // 添加 设备名、MADE IN g.drawString(content, 350, 110); g.drawString(mMade, 50, 340); } else { // 设置水印的坐标 标题 g.drawString(content, 50, 115); } g.dispose(); // 输出图片 FileOutputStream outImgStream = new FileOutputStream(targetImg); ImageIO.write(bufImg, "jpg", outImgStream); System.out.println("文字水印添加完成"); outImgStream.flush(); outImgStream.close(); }