Android實(shí)現(xiàn)拍照添加時(shí)間水印
本文實(shí)例為大家分享了Android實(shí)現(xiàn)拍照添加時(shí)間水印的具體代碼,供大家參考,具體內(nèi)容如下
效果如下圖 :
1、拍照
// 非空判斷 拍照 ?if (mCamera0 != null){ ? ? ? ? ? ? ? ? mCamera0.takePicture(null, null, jpeg0); // 1 front ? ? ? ? ? ? }
2、創(chuàng)建JPEG數(shù)據(jù)回調(diào)
// 創(chuàng)建JPEG圖片回調(diào)數(shù)據(jù)對(duì)象 public static PictureCallback jpeg0 = new PictureCallback() { ? ? ? ? @Override ? ? ? ? public void onPictureTaken(byte[] data, Camera camera) { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? // 獲得圖片 ? ? ? ? ? ? ? ? Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); ? ? ? ? ? ? ? ? //添加時(shí)間水印 ? ? ? ? ? ? ? ? Bitmap mTimeWatermark = AddTimeWatermark(mBitmap); ? ? ? ? ? ? ? ? // 判斷文件夾是否存在,傳入圖片存儲(chǔ)路徑 ? ? ? ? ? ? ? ? CreateFile(StaticVariables.mPicPath); ? ? ? ? ? ? ? ? ? String mPath = StaticVariables.mPicPath + System.currentTimeMillis()+".jpeg"; ? ? ? //臨時(shí)測(cè)試命名 ? ? ? ? ? ? ? ? ? File mFile = new File(mPath); ? ? ? ? ? ? ? ? ? BufferedOutputStream mOutputStream = new BufferedOutputStream(new FileOutputStream(mFile)); ? ? ? ? ? ? ? ? // 將圖片壓縮到流中 ? ? ? ? ? ? ? ? mTimeWatermark.compress(Bitmap.CompressFormat.JPEG,100,mOutputStream); ? ? ?//時(shí)間水印 ? ? ? ? ? ? ? ? ? mOutputStream.flush(); ? ? ? ? ? ? ? ? mOutputStream.close(); ? ? ? ? ? ? ? ? ? // 停止預(yù)覽 ? ? ? ? ? ? ? ? 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()); ? ? ? ? ? ? ? ? ? // 處理完數(shù)據(jù)之后預(yù)覽 ? ? ? ? ? ? ? ? mCamera0.startPreview(); ? ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? ? // TODO Auto-generated catch block ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? };
3、添加時(shí)間水?。篈ddTimeWatermark(mBitmap);
/** ? ? ?* 添加時(shí)間水印 ? ? ?* @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); ? ? ? ? //向位圖中開(kāi)始畫(huà)入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+" ?經(jīng)度:"+GpsService.longitude; ? ? ? ? mPaint.setColor(Color.RED); ? ? ? ? mPaint.setTextSize(20); ? ? ? ? //水印的位置坐標(biāo) ? ? ? ? mCanvas.drawText(mFormat, (mBitmapWidth * 1) / 10,(mBitmapHeight*14)/15,mPaint); // ? ? ? ?mCanvas.save(Canvas.ALL_SAVE_FLAG); ? ? ? ? mCanvas.save(); ? ? ? ? mCanvas.restore(); ? ? ? ? ? return mNewBitmap; }
4、創(chuàng)建文件夾
// 創(chuàng)建 文件夾 public static void CreateFile(String path) { ? ? ? ? File destDir = new File(path); ? ? ? ? if (!destDir.exists()) { ? ? ? ? ? ? Log.i(TAG,"文件夾創(chuàng)建成功"); ? ? ? ? ? ? destDir.mkdirs(); ? ? ? ? } else { ? ? ? ? ? ? Log.i(TAG,"文件夾已存在"); ? ? ? ? } ? ? }
5、Java 實(shí)現(xiàn)添加水印
/** ?? ? * 設(shè)置文字水印 ?? ? *? ?? ? * @param sourceImg ?? ? * ? ? ? ? ? ?源圖片路徑 ?? ? * @param targetImg ?? ? * ? ? ? ? ? ?保存的圖片路徑 ?? ? * @param content ?? ? * ? ? ? ? ? ?內(nèi)容 ?? ? * @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")) { ?? ??? ??? ?// 添加 設(shè)備名、MADE IN ?? ??? ??? ?g.drawString(content, 350, 110); ?? ??? ??? ?g.drawString(mMade, 50, 340); ?? ??? ?} else { ?? ??? ??? ?// 設(shè)置水印的坐標(biāo) 標(biāo)題 ?? ??? ??? ?g.drawString(content, 50, 115); ?? ??? ?} ?? ??? ?g.dispose(); ? ?? ??? ?// 輸出圖片 ?? ??? ?FileOutputStream outImgStream = new FileOutputStream(targetImg); ?? ??? ?ImageIO.write(bufImg, "jpg", outImgStream); ?? ??? ?System.out.println("文字水印添加完成"); ?? ??? ?outImgStream.flush(); ?? ??? ?outImgStream.close(); ?? ?}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android視頻處理之動(dòng)態(tài)時(shí)間水印效果
- Android添加水印的正確方法 只要三步!
- Android實(shí)現(xiàn)為圖片添加水印
- Android 給圖片加上水印的示例代碼(支持logo+文字)
- Android給任何view添加全屏傾斜水印
- Android 圖片添加水印的實(shí)現(xiàn)方法
- Android給圖片加文字和圖片水印實(shí)例代碼
- android實(shí)現(xiàn)文字水印效果 支持多行水印
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)分享長(zhǎng)圖并且添加全圖水印
相關(guān)文章
Android 實(shí)現(xiàn)文件夾排序功能的實(shí)例代碼
這篇文章主要介紹了Android 實(shí)現(xiàn)文件夾排序功能的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-09-09Android EditText 實(shí)現(xiàn)監(jiān)聽(tīng)實(shí)例
本文主要介紹Android EditText 組件 實(shí)現(xiàn)監(jiān)聽(tīng)事件,并附有代碼實(shí)例,在Android開(kāi)發(fā)過(guò)程中如果能用到可以參考下2016-07-07Android自定義recyclerView實(shí)現(xiàn)時(shí)光軸效果
這篇文章主要介紹了Android自定義recyclerView實(shí)現(xiàn)時(shí)光軸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01androidstudio3.0使用butterknife報(bào)錯(cuò)解決的解決方法
這篇文章主要介紹了androidstudio3.0使用butterknife報(bào)錯(cuò)解決的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-01-01