Tuesday, January 8, 2013

Reading exif info



How to get the Exif info of an image :
Private string  selected imagepath;

if (requestCode == IMAGE_CAPTURE) {
                if (resultCode == RESULT_OK) 
       imageName = String.valueOf(System.currentTimeMillis()) + ".jpg";
    Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
Log.w("Path of Capture image", "Camera Image Path :"+ selectedImagePath);
Getexifinfo();
}
}
public String getRealPathFromURI(Uri contentUri) {
                                                try {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                                                                cursor.moveToFirst();
                                                                return cursor.getString(column_index);
                                                } catch (Exception e) {
                                                                return contentUri.getPath();
                                                }}
private void getexifinfo() {
               
                                                try {
                                                                ExifInterface exif = new ExifInterface(selectedImagePath);        
StringBuilder builder = new StringBuilder();
                               
                                builder.append("Date & Time: " + getExifTag(exif,ExifInterface.TAG_DATETIME) + "\n\n");
                                builder.append("Flash: " + getExifTag(exif,ExifInterface.TAG_FLASH) + "\n");
                                builder.append("Focal Length: " + getExifTag(exif,ExifInterface.TAG_FOCAL_LENGTH) + "\n\n");
                                builder.append("GPS Datestamp: " + getExifTag(exif,ExifInterface.TAG_FLASH) + "\n");
                                builder.append("GPS Latitude: " + getExifTag(exif,ExifInterface.TAG_GPS_LATITUDE) + "\n");
                                builder.append("GPS Latitude Ref: " + getExifTag(exif,ExifInterface.TAG_GPS_LATITUDE_REF) + "\n");
                                builder.append("GPS Longitude: " + getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE) + "\n");
                                builder.append("GPS Longitude Ref: " + getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE_REF) + "\n");
                                builder.append("GPS Processing Method: " + getExifTag(exif,ExifInterface.TAG_GPS_PROCESSING_METHOD) + "\n");
                                builder.append("GPS Timestamp: " + getExifTag(exif,ExifInterface.TAG_GPS_TIMESTAMP) + "\n\n");
                                builder.append("Image Length: " + getExifTag(exif,ExifInterface.TAG_IMAGE_LENGTH) + "\n");
                                builder.append("Image Width: " + getExifTag(exif,ExifInterface.TAG_IMAGE_WIDTH) + "\n\n");
                                builder.append("Camera Make: " + getExifTag(exif,ExifInterface.TAG_MAKE) + "\n");
                                builder.append("Camera Model: " + getExifTag(exif,ExifInterface.TAG_MODEL) + "\n");
                                builder.append("Camera Orientation: " + getExifTag(exif,ExifInterface.TAG_ORIENTATION) + "\n");
                                builder.append("Camera White Balance: " + getExifTag(exif,ExifInterface.TAG_WHITE_BALANCE) + "\n");
                               
  TextView info = (TextView)findViewById(R.id.exifinfo);
                               
  info.setText(builder.toString());




(for latitude , longitude the values are retrieved as degrees ,minutes, seconds so we convert to degreee                )
                                                                Float Latitude, Longitude;
                                String attrLATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
//this gives the latitude in degrees minutes seconds
                                 String attrLATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
                 String attrLONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
 String attrLONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
//convert  to degrees
if(attrLATITUDE_REF.equals("N")){
                                                                                   Latitude = convertToDegree(attrLATITUDE);
                                                                                  }
                                                                                  else{
                                                                                   Latitude = 0 - convertToDegree(attrLATITUDE);
                                                                                  }
  if(attrLONGITUDE_REF.equals("E")){
   Longitude = convertToDegree(attrLONGITUDE);
                                                                                  }
                                                                                  else{
                                                                                   Longitude = 0 - convertToDegree(attrLONGITUDE);
                                                                                  }           
                Toast.makeText(getApplicationContext(),latitude+longitude+ Toast.LENGTH_LONG).show();
                                                } catch (IOException e) {
                                                                // TODO Auto-generated catch block
                                                                e.printStackTrace();
                                                }
               
                                }
               
                                private Float convertToDegree(String stringDMS){
                                                 Float result = null;
                                                 String[] DMS = stringDMS.split(",", 3);

                                                 String[] stringD = DMS[0].split("/", 2);
                                                    Double D0 = new Double(stringD[0]);
                                                    Double D1 = new Double(stringD[1]);
                                                    Double FloatD = D0/D1;

                                                 String[] stringM = DMS[1].split("/", 2);
                                                 Double M0 = new Double(stringM[0]);
                                                 Double M1 = new Double(stringM[1]);
                                                 Double FloatM = M0/M1;
                                                 
                                                 String[] stringS = DMS[2].split("/", 2);
                                                 Double S0 = new Double(stringS[0]);
                                                 Double S1 = new Double(stringS[1]);
                                                 Double FloatS = S0/S1;
                                                 
                                                    result = new Float(FloatD + (FloatM/60) + (FloatS/3600));
                                                 return result;
                                                };

No comments:

Post a Comment