拍照时增加时间戳到照片右下角

来源:互联网 发布:jade软件怎么用 编辑:程序博客网 时间:2024/04/29 06:00

方法1:使用rawdata数据生成bitmap文件,增加时间戳到bitmap,把新的bitmap数据保存到jpeg文件中

方法2: 保存文件成功后  打开文件生成bitmap文件,删除源文件,增加时间戳,覆盖源文件


方法1:修改文件 packages/apps/Camera/src/com/mediatek/camera/mode/PhotoMode.java

diff --git a/packages/apps/Camera/src/com/mediatek/camera/mode/PhotoMode.java b/packages/apps/Camera/src/com/mediatek/camera/mode/PhotoMode.java
index c16eac6..c8e0e04 100755
--- a/packages/apps/Camera/src/com/mediatek/camera/mode/PhotoMode.java
+++ b/packages/apps/Camera/src/com/mediatek/camera/mode/PhotoMode.java
@@ -78,6 +78,23 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 
+//jimbo add start for timestamp
+//import android.graphics.Bitmap;
+import android.os.SystemProperties;
+
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Typeface;
+import android.graphics.Bitmap.Config;
+import android.text.format.DateFormat;
+import android.graphics.Rect;
+
+import java.io.ByteArrayOutputStream;
+//jimbo add end for timestamp
+
+
 public class PhotoMode extends CameraMode implements FocusListener, ICameraAddition.Listener {
     private static final String TAG = "PhotoMode";
     
@@ -477,9 +494,32 @@ public class PhotoMode extends CameraMode implements FocusListener, ICameraAddit
                 }
             }

    private final PictureCallback mJpegPictureCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] jpegData, Camera camera) {

  ......

           // Calculate the width and the height of the jpeg.

-            if (!mIModuleCtrl.isImageCaptureIntent()) {
+           if (!mIModuleCtrl.isImageCaptureIntent()) {
+ //jimbo add start for timestamp
+      if (SystemProperties.getBoolean("ro.camera.timestamp.surpport", false) 
+   && "on".equals(mISettingCtrl.getSettingValue(SettingConstants.KEY_CAMERA_TS))) {  
+ //Log.i("jimbo", "[saveRequest] timestamp opened ");
+ try {
+ Bitmap bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
+ Bitmap markedBitmap = createBitmap(bitmap, mCaptureStartTime);
+ ByteArrayOutputStream markedJpegOut = new ByteArrayOutputStream();
+
+ markedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, markedJpegOut);
+
+ mIFileSaver.savePhotoFile(markedJpegOut.toByteArray(), null, mCaptureStartTime, true, mIModuleCtrl.getLocation(), 0, null);
+
+ markedBitmap.recycle();
+ markedBitmap = null;
+ }catch(Exception ex){
+ ex.printStackTrace();
+ Log.e(TAG, " IOException"  );
+ }
+       }else
+ //jimbo add end for timestamp
+       {
                 mIFileSaver.savePhotoFile(jpegData, null, mCaptureStartTime, true,
                         mIModuleCtrl.getLocation(), 0, null);
+       }
             } else {
                 mJpegImageData = jpegData;
                 if (!mIModuleCtrl.isQuickCapture()) {
@@ -497,6 +537,58 @@ public class PhotoMode extends CameraMode implements FocusListener, ICameraAddit
             mJpegPictureCallbackTime = 0;
         }
     };
+
+//jimbo add start for timestamp
+ public static Bitmap createBitmap(Bitmap src, long dateTaken) {
+        //
+        //String datetime = DateFormat.format("yyyy:MM:dd kk:mm:ss", dateTaken).toString();
+        String datetime = DateFormat.format("HH:mm dd-MM-yy", dateTaken).toString();
+//Log.i("jimbo", "[createBitmap]datetime = " + datetime + ", dateTaken = " + dateTaken);
+
+        //
+        int w = src.getWidth();
+        int h = src.getHeight();
+
+        Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
+        Canvas canvas = new Canvas(bmpTemp);
+        Paint p = new Paint();
+        String fontName = "serif";
+        int textSize = 60;
+        Typeface font = Typeface.create(fontName, Typeface.NORMAL);
+        p.setColor(Color.YELLOW);
+        p.setTypeface(font);
+ //shoud reset textSize accroding pictue size
+ if(w<240){
+ textSize = 10;
+ }else if(w<480){
+ textSize = 10;
+ }else if(w<768){
+ textSize = 20;
+ }else if(w<960){
+ textSize = 30;
+ }else if(w<1200){
+ textSize = 40;
+ }else if(w<1536){
+ textSize = 50;
+ }else if(w<1920){
+ textSize = 60;
+ }else{
+ textSize = 70;
+ }
+        p.setTextSize(textSize);
+        canvas.drawBitmap(src, 0, 0, p);
+
+ int markTextLen = (int)p.measureText(datetime);
+//Log.i("jimbo", "[createBitmap] w = " + w + ", h = " + h+", markTextLen="+markTextLen);
+
+        canvas.drawText(datetime, w-markTextLen - textSize, h-textSize, p);
+
+        //canvas.drawText(datetime, 0, textSize, p);
+        canvas.save(Canvas.ALL_SAVE_FLAG);
+        canvas.restore();
+        return bmpTemp;
+    }
+//jimbo add end for timestamp

......

}

     
方法2:修改文件  packages/apps/Camera/src/com/android/camera/FileSaver.java

diff --git a/packages/apps/Camera/src/com/android/camera/FileSaver.java b/packages/apps/Camera/src/com/android/camera/FileSaver.java
index 58b7c94..0c4f43e 100755
--- a/packages/apps/Camera/src/com/android/camera/FileSaver.java
+++ b/packages/apps/Camera/src/com/android/camera/FileSaver.java
@@ -63,6 +63,20 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
+//jimbo add start for timestamp
+//import android.graphics.Bitmap;
+import android.os.SystemProperties;
+
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Typeface;
+import android.graphics.Bitmap.Config;
+import android.text.format.DateFormat;
+import android.graphics.Rect;
+//jimbo add end for timestamp
+
 public class FileSaver {
     private static final String TAG = "FileSaver";
     
@@ -299,6 +313,79 @@ public class FileSaver {
         
         return name;
     }
+
+//jimbo add start for timestamp
+ public Bitmap createBitmap(Bitmap src, long dateTaken) {
+        //
+        //String datetime = DateFormat.format("yyyy:MM:dd kk:mm:ss", dateTaken).toString();
+        String datetime = DateFormat.format("HH:mm dd-MM-yy", dateTaken).toString();
+//Log.i("jimbo", "[createBitmap]datetime = " + datetime + ", dateTaken = " + dateTaken);
+
+        //
+        int w = src.getWidth();
+        int h = src.getHeight();
+
+        Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
+        Canvas canvas = new Canvas(bmpTemp);
+        Paint p = new Paint();
+        String fontName = "serif";
+        int textSize = 60;
+        Typeface font = Typeface.create(fontName, Typeface.NORMAL);
+        p.setColor(Color.YELLOW);
+        p.setTypeface(font);
+ //shoud reset textSize accroding pictue size
+ if(w<240){
+ textSize = 10;
+ }else if(w<480){
+ textSize = 10;
+ }else if(w<768){
+ textSize = 20;
+ }else if(w<960){
+ textSize = 30;
+ }else if(w<1200){
+ textSize = 40;
+ }else if(w<1536){
+ textSize = 50;
+ }else if(w<1920){
+ textSize = 60;
+ }else{
+ textSize = 70;
+ }
+        p.setTextSize(textSize);
+        canvas.drawBitmap(src, 0, 0, p);
+
+ int markTextLen = (int)p.measureText(datetime);
+Log.i("jimbo", "[createBitmap] w = " + w + ", h = " + h+", markTextLen="+markTextLen);
+
+        canvas.drawText(datetime, w-markTextLen - textSize, h-textSize, p);
+
+        //canvas.drawText(datetime, 0, textSize, p);
+        canvas.save(Canvas.ALL_SAVE_FLAG);
+        canvas.restore();
+        return bmpTemp;
+    }
+
+    //
+    public void compressAndSaveBitmap(Bitmap rawBitmap, String mFilePath, int quality){
+        File saveFile= new File(mFilePath);
+        if (saveFile.exists()) {
+            saveFile.delete();
+//Log.i("jimbo", "[compressAndSaveBitmap] delete oldfile : mFilePath = " + mFilePath);
+        }
+
+        try {
+            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
+            if (fileOutputStream != null) {
+                rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
+//Log.i("jimbo", "[compressAndSaveBitmap] save marked file: mFilePath = " + mFilePath);
+            }
+            fileOutputStream.flush();
+            fileOutputStream.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+//jimbo add end for timestamp
     
     private abstract class RequestOperator implements SaveRequest {
         String mTitle;
@@ -636,6 +723,28 @@ public class FileSaver {
             mFilePath = Storage.generateFilepath(mFileName);
             mTempFilePath = mFilePath + TEMP_SUFFIX;
             saveImageToSDCard(mTempFilePath, mFilePath, mData);


    private class PhotoOperator extends RequestOperator {

......

        @Override
        public synchronized void saveRequest() {

......
+
+ //jimbo add start for timestamp
+      if (SystemProperties.getBoolean("ro.camera.timestamp.surpport", false)) {  
+ boolean bTimeStampEnabled = "on".equals(SystemProperties.get("persist.camera.ts.enable", "off"));  
+ Log.i("jimbo", "[saveRequest]bTimeStampEnabled = " + bTimeStampEnabled);
+
+ if(bTimeStampEnabled){
+ try {
+ ExifInterface exif = new ExifInterface(mFilePath);
+ String model = exif.getAttribute(ExifInterface.TAG_MODEL);    
+ Bitmap bitmap = BitmapFactory.decodeFile(mFilePath, null);
+ Bitmap markbitmap = createBitmap(bitmap, mDateTaken);
+ compressAndSaveBitmap(markbitmap, mFilePath, 80);
+ exif.saveAttributes();
+ }catch(IOException ex){
+ ex.printStackTrace();
+ Log.e(TAG, " IOException"  );
+ }
+ }
+       }
+ //jimbo add end for timestamp
+
             // camera decouple
             if (mNeedSaveToDB) {
                 mMimeType = Storage.generateMimetype(mTitle, mTempPictureType);   


}// saveRequest

}//PhotoOperator 


效果图:



0 0
原创粉丝点击