Using exif to set location to captured image

来源:互联网 发布:淘宝管控下架什么意思 编辑:程序博客网 时间:2024/06/07 18:26
android自动保存相机的属性与方向
camera方向的获取
int oldOrientation = -1;
 mOrientationListener = new OrientationEventListener(Camera.this) {
            @Override
            public void onOrientationChanged(int orientation) {
                      int oldOrientation = util.ruondOrientation(orientation,oldOrientation);
            }
        };
四个方向的确定
package com.example.cameraapp;
public class util{
public int roundOrientation(int orientation, int oldOrientation){
boolean changeOrientation = false;
if(oldOrientation == OrientationEventListener.ORIENTATION_UNKNOWN){
changeOrientation = true;
} else {
int dest = Math.abs(orientation-oldOrientation);
dest = Math.min(dest,360-dest);
changeOrientation = (dest>=45+ORIENTATION_HYSTERESIS);
}
if(changeOrientation ){
return((orientation +45)/90*90)%360;
}
return oldOrientation;
}
}
package com.example.cameraapp;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.media.ExifInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class CameraAppActivity extends Activity{ private static final int CAMERA_REQUEST = 1888;     private ImageButton photoButton;    private ImageView photoPic;    private TextView Exif;    Button updateDummyLoc;    ExifInterface exifInterface;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    photoButton = (ImageButton) this.findViewById(R.id.button1);        photoPic = (ImageView) this.findViewById(R.id.photo1);      Exif = (TextView)findViewById(R.id.exif);    photoButton.setOnClickListener(new View.OnClickListener() {         public void onClick(View v) {  //The method that is called when button is clicked,                                        //as part of the OnClickListener interface.             Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);              startActivityForResult(cameraIntent, CAMERA_REQUEST);             updateDummyLoc = (Button)findViewById(R.id.update);             updateDummyLoc.setOnClickListener(new Button.OnClickListener(){             public void onClick(View arg0) {              // TODO Auto-generated method stub              UpdateExit();             }});         }});    }protected void onActivityResult(int requestCode, int resultCode, String data) {      if (requestCode == CAMERA_REQUEST) {      //  Bitmap photo = (Bitmap) data.getExtras().get("data");         Bitmap photo = BitmapFactory.decodeFile(data);        photoPic.setImageBitmap(photo);        Exif.setText(ReadExif(data));    }  } void UpdateExit(){    String dummyLATITUDE = "0/1,16/1,5935883/125557";    String dummyLATITUDE_REF = "N";    String dummyLONGITUDE = "0/1,7/1,1429891026/483594097";    String dummyLONGITUDE_REF = "E"; exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, dummyLATITUDE); exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, dummyLATITUDE_REF); exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, dummyLONGITUDE); exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, dummyLONGITUDE_REF);    try {      exifInterface.saveAttributes();      Toast.makeText(CameraAppActivity.this,        "saveAttribute finished",        Toast.LENGTH_LONG).show();    }     catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();      Toast.makeText(CameraAppActivity.this,        e.toString(),        Toast.LENGTH_LONG).show();    }}String ReadExif(String data){        String exif="Exif: " + data;        try {      exifInterface = new ExifInterface(data);      exif += "\nIMAGE_LENGTH: " +  exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);      exif += "\nIMAGE_WIDTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);      exif += "\n DATETIME: " + exifInterface.getAttribute(ExifInterface.TAG_DATETIME);      exif += "\n TAG_MAKE: " + exifInterface.getAttribute(ExifInterface.TAG_MAKE);      exif += "\n TAG_MODEL: " + exifInterface.getAttribute(ExifInterface.TAG_MODEL);      exif += "\n TAG_ORIENTATION: " + exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);      exif += "\n TAG_WHITE_BALANCE: " + exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);      exif += "\n TAG_FOCAL_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);      exif += "\n TAG_FLASH: " + exifInterface.getAttribute(ExifInterface.TAG_FLASH);      exif += "\nGPS related:";      float[] LatLong = new float[2];      if(exifInterface.getLatLong(LatLong)){       exif += "\n latitude= " + LatLong[0];       exif += "\n longitude= " + LatLong[1];      }else{       exif += "Exif tags are not available!";      }      Toast.makeText(CameraAppActivity.this,        "finished",        Toast.LENGTH_LONG).show();     } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();      Toast.makeText(CameraAppActivity.this,        e.toString(),        Toast.LENGTH_LONG).show();     }        return exif;       }}
原创粉丝点击