在使用Camera API Android拍摄的图像上添加当前时间戳

时间:2015-11-28 11:21:07

标签: 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新手,无法识别实际问题。

2 个答案:

答案 0 :(得分:2)

以下行将目标位图绘制到自身上,因为destcs Canvas对象的支持位图。

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

相反,您希望绘制刚从文件中加载的位图。

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

答案 1 :(得分:0)

将此行更改为

Bitmap bitmap = BitmapFactory.decodeFile(getOutputMediaFile()。getAbsolutePath(),options);