Using exif to set location to captured image

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. */
@Override
public 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;
       }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值