private void setInfoFromPict(Uri imageUri) { Bitmap resizedBitmap = RamenUtil.getResizedBitmap(imageUri, this); ((ImageView) findViewById(R.id.img_ramenpict)).setImageBitmap(resizedBitmap); ExifInterface exifInterface = null; try { ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(imageUri, null, null, null, null); cursor.moveToFirst(); String filepath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)); exifInterface = new ExifInterface(filepath); } catch (Exception e) { e.printStackTrace(); } float[] latLong = new float[2]; exifInterface.getLatLong(latLong); if (latLong[0] != 0.0f || latLong[1] != 0.0f) { ((EditText) findViewById(R.id.txt_lat)).setText(String.valueOf(latLong[0])); ((EditText) findViewById(R.id.txt_lon)).setText(String.valueOf(latLong[1])); Toast.makeText(this, "写真の位置情報を自動入力しました", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写真の位置情報が含まれていません。経度緯度を入力してください。", Toast.LENGTH_SHORT).show(); } }
/** * Read location information from image. * * @param imagePath : image absolute path * @return : loation information */ public Location readGeoTagImage(String imagePath) { Location loc = new Location(""); try { ExifInterface exif = new ExifInterface(imagePath); float[] latlong = new float[2]; if (exif.getLatLong(latlong)) { loc.setLatitude(latlong[0]); loc.setLongitude(latlong[1]); } String date = exif.getAttribute(ExifInterface.TAG_DATETIME); SimpleDateFormat fmt_Exif = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); try { loc.setTime(fmt_Exif.parse(date).getTime()); } catch (java.text.ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return loc; }
private void setInfoFromPict(Uri imageUri) { ExifInterface exifInterface = null; try { ContentResolver resolver = getContentResolver(); InputStream in = resolver.openInputStream(imageUri); Bitmap bitmap = BitmapFactory.decodeStream(in); in.close(); // 画面サイズを取得する Matrix matrix = new Matrix(); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int srcWidth = bitmap.getWidth(); // 元画像のwidth int srcHeight = bitmap.getHeight(); // 元画像のheight float screenWidth = (float) metrics.widthPixels; float screenHeight = (float) metrics.heightPixels; float widthScale = screenWidth / srcWidth; float heightScale = screenHeight / srcHeight; if (widthScale > heightScale) { matrix.postScale(heightScale, heightScale); } else { matrix.postScale(widthScale, widthScale); } Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, srcWidth, srcHeight, matrix, true); ((ImageView) findViewById(R.id.img_update_ramenpict)).setImageBitmap(resizedBitmap); Cursor cursor = resolver.query(imageUri, null, null, null, null); cursor.moveToFirst(); String filepath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)); exifInterface = new ExifInterface(filepath); } catch (Exception e) { e.printStackTrace(); } float[] latLong = new float[2]; exifInterface.getLatLong(latLong); if (latLong[0] != 0.0f || latLong[1] != 0.0f) { ((EditText) findViewById(R.id.txt_update_lat)).setText(String.valueOf(latLong[0])); ((EditText) findViewById(R.id.txt_update_lon)).setText(String.valueOf(latLong[1])); Toast.makeText(this, "写真の位置情報を自動入力しました", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "写真の位置情報が含まれていません。経度緯度を入力してください。", Toast.LENGTH_SHORT).show(); } }
private static void setLatLngDetails(final View d, Activity context, ExifInterface exif) { float[] latlng = new float[2]; if (exif.getLatLong(latlng)) { setDetailsValue(d, String.valueOf(latlng[0]), R.id.details_latitude_value); setDetailsValue(d, String.valueOf(latlng[1]), R.id.details_longitude_value); if (latlng[0] == INVALID_LATLNG || latlng[1] == INVALID_LATLNG) { hideDetailsRow(d, R.id.details_latitude_row); hideDetailsRow(d, R.id.details_longitude_row); hideDetailsRow(d, R.id.details_location_row); return; } UpdateLocationCallback cb = new UpdateLocationCallback(new WeakReference<View>(d)); Geocoder geocoder = new Geocoder(context); new ReverseGeocoderTask(geocoder, latlng, cb).execute(); } else { hideDetailsRow(d, R.id.details_latitude_row); hideDetailsRow(d, R.id.details_longitude_row); hideDetailsRow(d, R.id.details_location_row); } }
@Override protected Void doInBackground(Void... params) { // File directory = new File("/storage/emulated/0/DCIM/Camera");//1396798512418.jpg // File directory1 = // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);//+"/Camera"); File directory = HomeActivity.EMULSIFY_DIRECTORY; // new File(directory1.getAbsolutePath() + "/emulsify"); // File directory = new File(Environment.DIRECTORY_DCIM+"/camera/"); File[] files = directory.listFiles(); if (files != null) { for (File f : files) { Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath()); Bitmap bmp2 = null; if (bmp.getWidth() > bmp.getHeight()) bmp2 = Bitmap.createScaledBitmap( bmp, (int) (((float) bmp.getWidth() / bmp.getHeight()) * ICON_HEIGHT), ICON_HEIGHT, false); else if (bmp.getWidth() < bmp.getHeight()) bmp2 = Bitmap.createScaledBitmap( bmp, ICON_HEIGHT, (int) (((float) bmp.getHeight() / bmp.getWidth()) * ICON_HEIGHT), false); float Latitude = 0.0F, Longitude = 0.0F; ExifInterface exif = null; try { exif = new ExifInterface(f.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } // TODO: credit // http://stackoverflow.com/questions/15403797/how-to-get-the-latititude-and-longitude-of-an-image-in-sdcard-to-my-application String LATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE); String LATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); String LONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); String LONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); if ((LATITUDE != null) && (LATITUDE_REF != null) && (LONGITUDE != null) && (LONGITUDE_REF != null)) { if (LATITUDE_REF.equals("N")) { Latitude = convertToDegree(LATITUDE); } else { Latitude = 0 - convertToDegree(LATITUDE); } if (LONGITUDE_REF.equals("E")) { Longitude = convertToDegree(LONGITUDE); } else { Longitude = 0 - convertToDegree(LONGITUDE); } publishProgress(bmp2, Latitude, Longitude, f.getAbsolutePath()); } else { // try the second approach, which is not used by emulsify pictures (thus hopefully // allowing for // the easy assimilation of ANY picture put in the emulsify directory) float[] d = new float[2]; exif.getLatLong(d); Latitude = d[0]; Longitude = d[1]; publishProgress(bmp2, Latitude, Longitude, f.getAbsolutePath()); } } } else { emptyFlag = true; } return null; }
public static boolean hasLatLngData(IImage image) { ExifInterface exif = getExif(image); if (exif == null) return false; float latlng[] = new float[2]; return exif.getLatLong(latlng); }