为JPEG图片添加自定义标签(Android)

来源:互联网 发布:flash mac版 编辑:程序博客网 时间:2024/06/08 13:28

EXIFExchangeable image file format)为可交换图像文件的简称,是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据,EXIF可以附加于JPEG、TIFF、RIFF等文件之中(维基百科)。

有时候我们想在拍摄的图像中加入自定义的一些标注,而Andord SDK的Reference中的ExifInterface (http://developer.android.com/reference/android/media/ExifInterface.html)并没有包含所有的EXIF标签接口,我们可以用下面的方法把自定义的信息填到userComment这一标签里。

Button.OnClickListener takePicBtnListener = new Button.OnClickListener() {    //@Overridepublic void onClick(View v) {Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);UUID id = UUID.randomUUID();imgPath = rootPath + id.toString() + ".jpg";File imgFile = new File(imgPath);try {if(!imgFile.exists())imgFile.createNewFile();takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));} catch (IOException e) {e.printStackTrace();imgFile = null;}startActivityForResult(takePictureIntent,0);}};Button.OnClickListener showUsrCmtBtnListener = new Button.OnClickListener() {//@Overridepublic void onClick(View v) {try {ExifInterface exif = new ExifInterface(imgPath);showDialog(exif.getAttribute("UserComment"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == RESULT_OK) {try {ExifInterface exif = new ExifInterface(imgPath);String mString = "Beijing";     exif.setAttribute("UserComment", mString);exif.saveAttributes();}catch(IOException e){e.printStackTrace();}}}private void showDialog(String msg)    {    AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage(msg);AlertDialog alert = builder.create();alert.show();    }
注:代码中的“UserComment”字段不能随意更改(如改成“UserCmt”),因为“UserComment”是EXIF的标准tag。