2015-11-28 36 views
0

我使用相機內置的回調攝像頭捕獲圖像我下拍攝的圖像當前時間戳:添加使用相機API的Android

jpegCallback = new PictureCallback() { 

     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      new Thread(new SavePicThread(data)).start(); 
      refreshCamera(); 
     } 
    }; 

public class SavePicThread implements Runnable { 
    byte[] data; 
    public SavePicThread(byte[] data) { 
     this.data = data; 
    } 
    public void run() { 
     // make a new picture file 
     File pictureFile = getOutputMediaFile(); 

     if (pictureFile == null) { 
      return; 
     } 
     try { 
      // write to the file 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(data); 
      fos.flush(); 
      fos.close(); 
      ShowCamera.this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        timestampItAndSave(); 
       } 

      }); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // make the picture visible to the rest of the device 
     galleryAddPic(pictureFile); 
     Intent i = new Intent(ShowCamera.this, UploadActivity.class); 
     i.putExtra("filePath", pictureFile.getPath()); 
     startActivity(i); 
    } 
} 

// make picture and save to a folder 
private File getOutputMediaFile() { 
    // make a new file directory inside the "sdcard" folder 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "imgCaptureApp"); 

    // if the directory does not exist 
    if (!mediaStorageDir.exists()) { 
     // if you cannot make this directory return 
     if (!mediaStorageDir.mkdirs()) { 
      return null; 
     } 
    } 

    // take the current timeStamp 
    String timeStamp = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss").format(new Date()); 
    File mediaFile; 
    // and make a media file: 
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); 
    return mediaFile; 
} 

保存文件後,我再次調用它,它時間戳。但它會生成帶有時間戳的黑色圖像。無法確定我做錯了什麼。

private void timestampItAndSave(){ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap bitmap = BitmapFactory.decodeFile(getOutputMediaFile().getAbsolutePath()); 

    //  Bitmap src = BitmapFactory.decodeResource(); // the original file is cuty.jpg i added in resources 
    Bitmap dest = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system 

    Canvas cs = new Canvas(dest); 
    Paint tPaint = new Paint(); 
    tPaint.setTextSize(35); 
    tPaint.setColor(Color.BLUE); 
    tPaint.setStyle(Paint.Style.FILL); 
    cs.drawBitmap(dest, 0f, 0f, null); 
    float height = tPaint.measureText("yY"); 
    cs.drawText(dateTime, 20f, height+15f, tPaint); 
    try { 
     dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg"))); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

如果我可以在保存圖像之前對圖像進行時間戳記,那會更好。搜索後,我找到了我已經實現的代碼,但由於我是android新手,無法確定實際問題。

+0

如何將它設置爲圖像底部? –

回答

1

以下一行正在將目標位圖繪製到自身上,因爲dest是Canvas對象的後備位圖。

cs.drawBitmap(dest, 0f, 0f, null); 

相反,您要繪製剛從文件中加載的位圖。

cs.drawBitmap(bitmap, 0f, 0f, null);