List of usage examples for android.media ExifInterface TAG_ORIENTATION
String TAG_ORIENTATION
To view the source code for android.media ExifInterface TAG_ORIENTATION.
Click Source Link
From source file:com.yanzhenjie.album.task.LocalImageLoader.java
/** * Read the rotation angle of the picture file. * * @param path image path./*from w w w. ja v a 2s . c om*/ * @return one of 0, 90, 180, 270. */ public static int readDegree(String path) { try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: return 90; case ExifInterface.ORIENTATION_ROTATE_180: return 180; case ExifInterface.ORIENTATION_ROTATE_270: return 270; default: return 0; } } catch (Exception e) { return 0; } }
From source file:org.planetmono.dcuploader.ActivityUploader.java
public int queryExifOrientation(String path) { int orientation = 0; if (exifConstructor == null) { try {//w ww.j ava 2s . c o m exifConstructor = ExifInterface.class.getConstructor(new Class[] { String.class }); exifGetAttributeInt = ExifInterface.class.getMethod("getAttributeInt", new Class[] { String.class, int.class }); } catch (NoSuchMethodException e) { return 0; } } Integer o = 0; try { Object obj = exifConstructor.newInstance(new Object[] { path }); o = (Integer) exifGetAttributeInt.invoke(obj, ExifInterface.TAG_ORIENTATION, new Integer(0)); } catch (Exception e) { return 0; } Log.d(Application.TAG, "orientation " + o); switch (o) { case ExifInterface.ORIENTATION_ROTATE_90: orientation = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: orientation = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: orientation = 270; } return orientation; }
From source file:com.silentcircle.common.util.ViewUtil.java
/** * Finds JPEG image rotation flags from exif and returns matrix to be used to rotate image. * * @param fileName JPEG image file name. * * @return Matrix to use in image rotate transformation or null if parsing failed. *//*from w w w . jav a2 s. co m*/ public static Matrix getRotationMatrixFromExif(final String fileName) { Matrix matrix = null; try { ExifInterface exif = new ExifInterface(fileName); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotate = 0; switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotate = ROTATE_90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = ROTATE_180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotate = ROTATE_270; break; } if (rotate != 0) { matrix = new Matrix(); matrix.preRotate(rotate); } } catch (IOException e) { Log.i(TAG, "Failed to determine image flags from file " + fileName); } return matrix; }
From source file:com.spoiledmilk.ibikecph.util.Util.java
public static Bitmap bmpDecodeFile(File f, int width_limit, int height_limit, long max_size, boolean max_dimensions) { if (f == null) { return null; }//from www.j ava 2 s . co m LOG.d("bmpDecodeFile(" + f.getAbsolutePath() + "," + width_limit + "," + height_limit + "," + max_size + "," + max_dimensions + ")"); Bitmap bmp = null; boolean shouldReturn = false; FileInputStream fin = null; int orientation = ExifInterface.ORIENTATION_NORMAL; try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; fin = new FileInputStream(f); BitmapFactory.decodeStream(fin, null, o); try { fin.close(); fin = null; } catch (IOException e) { } // Find the correct scale value. It should be the power of 2. int scale = 1; if (width_limit != -1 && height_limit != -1) { if (max_dimensions) { while (o.outWidth / scale > width_limit || o.outHeight / scale > height_limit) scale *= 2; } else { while (o.outWidth / scale / 2 >= width_limit && o.outHeight / scale / 2 >= height_limit) scale *= 2; } } else if (max_size != -1) while ((o.outWidth * o.outHeight) / (scale * scale) > max_size) scale *= 2; // Decode with inSampleSize o = null; if (scale > 1) { o = new BitmapFactory.Options(); o.inSampleSize = scale; } fin = new FileInputStream(f); try { bmp = BitmapFactory.decodeStream(fin, null, o); } catch (OutOfMemoryError e) { // Try to recover from out of memory error - but keep in mind // that behavior after this error is // undefined, // for example more out of memory errors might appear in catch // block. if (bmp != null) bmp.recycle(); bmp = null; System.gc(); LOG.e("Util.bmpDecodeFile() OutOfMemoryError in decodeStream()! Trying to recover..."); } if (bmp != null) { LOG.d("resulting bitmap width : " + bmp.getWidth() + " height : " + bmp.getHeight() + " size : " + (bmp.getRowBytes() * bmp.getHeight())); ExifInterface exif = new ExifInterface(f.getPath()); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); } } catch (FileNotFoundException e) { shouldReturn = true; } catch (IOException e) { shouldReturn = true; // bitmap is still valid here, just can't be // rotated } finally { if (fin != null) try { fin.close(); } catch (IOException e) { } } if (shouldReturn || bmp == null) return bmp; float rotate = 0; switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; } if (rotate > 0) { Matrix matrix = new Matrix(); matrix.postRotate(rotate); Bitmap bmpRot = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); matrix = null; bmp.recycle(); bmp = null; // System.gc(); return bmpRot; } return bmp; }
From source file:com.example.photoremark.MainActivity.java
/** * ?exif?//from ww w . jav a 2s.co m * * @param path * @return */ public int getPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; }
From source file:me.tipi.kiosk.ui.fragments.OCRFragment.java
@Override public void onMetadataAvailable(Metadata metadata) { // This method will be called when metadata becomes available during recognition process. // Here, for every metadata type that is allowed through metadata settings, // desired actions can be performed. // detection metadata contains detection locations if (metadata instanceof DetectionMetadata) { // detection location is written inside DetectorResult DetectorResult detectorResult = ((DetectionMetadata) metadata).getDetectionResult(); // DetectorResult can be null - this means that detection has failed if (detectorResult == null) { if (mQvManager != null) { // begin quadrilateral animation to its default position // (internally displays FAIL status) mQvManager.animateQuadToDefaultPosition(); }/* www. j ava 2 s. co m*/ // when points of interested have been detected (e.g. QR code), this will be returned as PointsDetectorResult } else if (detectorResult instanceof QuadDetectorResult) { // begin quadrilateral animation to detected quadrilateral mQvManager.animateQuadToDetectionPosition((QuadDetectorResult) detectorResult); } } else if (metadata instanceof ImageMetadata) { // obtain image // Please note that Image's internal buffers are valid only // until this method ends. If you want to save image for later, // obtained a cloned image with image.clone(). if (guest.passportPath == null || TextUtils.isEmpty(guest.passportPath) || this.results == null) { Image image = ((ImageMetadata) metadata).getImage(); if (image != null && image.getImageType() == ImageType.SUCCESSFUL_SCAN) { Timber.w("Metadata type is successful frame image, we go to save it"); image.clone(); Timber.w("Ocr meta data captured"); final Bitmap bitmap = image.convertToBitmap(); Timber.w("Metadata converted to bitmap"); final Uri photoUri = ImageUtility.savePassportPicture(getActivity(), bitmap); Timber.w("OCR Uri got back from file helper with path: %s", photoUri != null ? photoUri.getPath() : "NO OCR FILE PATH!!!!!"); ExifInterface ei = null; try { ei = new ExifInterface(photoUri.getPath()); } catch (IOException e) { e.printStackTrace(); } int orientation = ei != null ? ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED) : 0; Timber.w("Ocr image orientation angle is: %d", orientation); Bitmap rotated = null; switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotated = rotateImage(bitmap, 90); break; case ExifInterface.ORIENTATION_ROTATE_180: rotated = rotateImage(bitmap, 180); break; case ExifInterface.ORIENTATION_ROTATE_270: rotated = rotateImage(bitmap, 270); break; case ExifInterface.ORIENTATION_UNDEFINED: rotated = rotateImage(bitmap, 90); break; case ExifInterface.ORIENTATION_NORMAL: default: break; } final Uri rotatedUri = ImageUtility.savePassportPicture(getActivity(), rotated); Timber.w("OCR ROTATED Uri got back from file helper with path: %s", rotatedUri != null ? rotatedUri.getPath() : "NO OCR FILE PATH!!!!!"); guest.passportPath = rotatedUri.getPath(); Timber.w("Ocr path saved with path: %s", rotatedUri.getPath()); } } else { Timber.w("We have ocr path and it's : %s", guest.passportPath); } // to convert the image to Bitmap, call image.convertToBitmap() // after this line, image gets disposed. If you want to save it // for later, you need to clone it with image.clone() } }
From source file:org.linphone.ContactEditorFragment.java
private void editContactPicture(final String filePath, final Bitmap image) { int SIZE_SMALL = 256; int COMPRESSOR_QUALITY = 100; Bitmap bitmapUnknown = BitmapFactory.decodeResource(getResources(), R.drawable.avatar); Bitmap bm = null;/*from w w w . ja va 2s. com*/ if (filePath != null) { int pixelsMax = SIZE_SMALL; //Resize image bm = BitmapFactory.decodeFile(filePath); if (bm != null) { if (bm.getWidth() > bm.getHeight() && bm.getWidth() > pixelsMax) { bm = Bitmap.createScaledBitmap(bm, 256, 256, false); } } } else if (image != null) { bm = image; } // Rotate the bitmap if possible/needed, using EXIF data try { if (imageToUploadUri != null && filePath != null) { ExifInterface exif = new ExifInterface(filePath); int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); Matrix matrix = new Matrix(); if (pictureOrientation == 6) { matrix.postRotate(90); } else if (pictureOrientation == 3) { matrix.postRotate(180); } else if (pictureOrientation == 8) { matrix.postRotate(270); } bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); } } catch (Exception e) { e.printStackTrace(); } Bitmap bitmapRounded; if (bm != null) { bitmapRounded = Bitmap.createScaledBitmap(bm, bitmapUnknown.getWidth(), bitmapUnknown.getWidth(), false); Canvas canvas = new Canvas(bitmapRounded); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(bitmapRounded, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); canvas.drawCircle(bitmapRounded.getWidth() / 2 + 0.7f, bitmapRounded.getHeight() / 2 + 0.7f, bitmapRounded.getWidth() / 2 + 0.1f, paint); contactPicture.setImageBitmap(bitmapRounded); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, COMPRESSOR_QUALITY, outStream); photoToAdd = outStream.toByteArray(); } }
From source file:org.egov.android.view.activity.CreateComplaintActivity.java
public String compressImage(String fromfilepath, String tofilepath) { Bitmap scaledBitmap = null;/*from w w w . j a v a 2 s . c om*/ BitmapFactory.Options options = new BitmapFactory.Options(); // by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If // you try the use the bitmap here, you will get null. options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(fromfilepath, options); int actualHeight = options.outHeight; int actualWidth = options.outWidth; // max Height and width values of the compressed image is taken as 816x612 float maxHeight = 816.0f; float maxWidth = 612.0f; float imgRatio = actualWidth / actualHeight; float maxRatio = maxWidth / maxHeight; // width and height values are set maintaining the aspect ratio of the image if (actualHeight > maxHeight || actualWidth > maxWidth) { if (imgRatio < maxRatio) { imgRatio = maxHeight / actualHeight; actualWidth = (int) (imgRatio * actualWidth); actualHeight = (int) maxHeight; } else if (imgRatio > maxRatio) { imgRatio = maxWidth / actualWidth; actualHeight = (int) (imgRatio * actualHeight); actualWidth = (int) maxWidth; } else { actualHeight = (int) maxHeight; actualWidth = (int) maxWidth; } } // setting inSampleSize value allows to load a scaled down version of the original image options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); // inJustDecodeBounds set to false to load the actual bitmap options.inJustDecodeBounds = false; // this options allow android to claim the bitmap memory if it runs low on memory options.inPurgeable = true; options.inInputShareable = true; options.inTempStorage = new byte[16 * 1024]; try { // load the bitmap from its path bmp = BitmapFactory.decodeFile(tofilepath, options); } catch (OutOfMemoryError exception) { exception.printStackTrace(); } try { scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); } catch (OutOfMemoryError exception) { exception.printStackTrace(); } float ratioX = actualWidth / (float) options.outWidth; float ratioY = actualHeight / (float) options.outHeight; float middleX = actualWidth / 2.0f; float middleY = actualHeight / 2.0f; Matrix scaleMatrix = new Matrix(); scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); Canvas canvas = new Canvas(scaledBitmap); canvas.setMatrix(scaleMatrix); canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG)); String attrLatitute = null; String attrLatituteRef = null; String attrLONGITUDE = null; String attrLONGITUDEREf = null; // check the rotation of the image and display it properly ExifInterface exif; try { exif = new ExifInterface(fromfilepath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); Log.d("EXIF", "Exif: " + orientation); Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 3) { matrix.postRotate(180); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 8) { matrix.postRotate(270); Log.d("EXIF", "Exif: " + orientation); } attrLatitute = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE); attrLatituteRef = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); attrLONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); attrLONGITUDEREf = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); } catch (IOException e) { e.printStackTrace(); } FileOutputStream out = null; try { out = new FileOutputStream(tofilepath); // write the compressed bitmap at the destination specified by filename. scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); ExifInterface exif2 = new ExifInterface(tofilepath); if (attrLatitute != null) { exif2.setAttribute(ExifInterface.TAG_GPS_LATITUDE, attrLatitute); exif2.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, attrLONGITUDE); } if (attrLatituteRef != null) { exif2.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, attrLatituteRef); exif2.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, attrLONGITUDEREf); } exif2.saveAttributes(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tofilepath; }
From source file:mobisocial.bento.todo.ui.TodoListFragment.java
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (DEBUG)// ww w . j a v a2 s.c om Log.d(TAG, "requestCode: " + requestCode + ", resultCode: " + resultCode); if (resultCode == Activity.RESULT_OK) { try { File imageFile = null; if (requestCode == REQUEST_IMAGE_CAPTURE) { imageFile = JpgFileHelper.getTmpFile(); } else if (requestCode == REQUEST_GALLERY) { Uri uri = data.getData(); if (uri == null || uri.toString().length() == 0) { return; } if (DEBUG) Log.d(TAG, "data URI: " + uri.toString()); ContentResolver cr = getActivity().getContentResolver(); String[] columns = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME }; Cursor c = cr.query(uri, columns, null, null, null); if (c != null && c.moveToFirst()) { if (c.getString(0) != null) { //regular processing for gallery files imageFile = new File(c.getString(0)); } else { final InputStream is = getActivity().getContentResolver().openInputStream(uri); imageFile = JpgFileHelper.saveTmpFile(is); is.close(); } } else if (uri.toString().startsWith("content://com.android.gallery3d.provider")) { // motorola xoom doesn't work for contentresolver even if the image comes from picasa. // So just adds the condition of startsWith... // See in detail : http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/ uri = Uri.parse( uri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d")); final InputStream is = getActivity().getContentResolver().openInputStream(uri); imageFile = JpgFileHelper.saveTmpFile(is); is.close(); } else { // http or https HttpURLConnection http = null; URL url = new URL(uri.toString()); http = (HttpURLConnection) url.openConnection(); http.setRequestMethod("GET"); http.connect(); final InputStream is = http.getInputStream(); imageFile = JpgFileHelper.saveTmpFile(is); is.close(); if (http != null) http.disconnect(); } } if (imageFile.exists() && imageFile.length() > 0) { if (DEBUG) Log.d(TAG, "imageFile exists=" + imageFile.exists() + " length=" + imageFile.length() + " path=" + imageFile.getPath()); float degrees = 0; try { ExifInterface exif = new ExifInterface(imageFile.getPath()); switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) { case ExifInterface.ORIENTATION_ROTATE_90: degrees = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degrees = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degrees = 270; break; default: degrees = 0; break; } } catch (IOException e) { e.printStackTrace(); } Bitmap bitmap = BitmapHelper.getResizedBitmap(imageFile, BitmapHelper.MAX_IMAGE_WIDTH, BitmapHelper.MAX_IMAGE_HEIGHT, degrees); TodoListItem item = new TodoListItem(); item.uuid = UUID.randomUUID().toString(); item.title = ""; item.description = ""; item.bDone = false; item.hasImage = true; item.creDateMillis = System.currentTimeMillis(); item.modDateMillis = System.currentTimeMillis(); item.creContactId = mManager.getLocalContactId(); item.modContactId = mManager.getLocalContactId(); StringBuilder msg = new StringBuilder( getString(R.string.feed_msg_added_photo, mManager.getLocalName())); String plainMsg = UIUtils.getPlainString(mManager.getBentoListItem().bento.name, msg.toString()); mManager.addTodo(item, bitmap, plainMsg); refreshView(); JpgFileHelper.deleteTmpFile(); } } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.klinker.android.twitter.activities.compose.Compose.java
private Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException { InputStream input = getContentResolver().openInputStream(uri); int reqWidth = 150; int reqHeight = 150; byte[] byteArr = new byte[0]; byte[] buffer = new byte[1024]; int len;//from w w w . j a v a2 s . co m int count = 0; try { while ((len = input.read(buffer)) > -1) { if (len != 0) { if (count + len > byteArr.length) { byte[] newbuf = new byte[(count + len) * 2]; System.arraycopy(byteArr, 0, newbuf, 0, count); byteArr = newbuf; } System.arraycopy(buffer, 0, byteArr, count, len); count += len; } } final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(byteArr, 0, count, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inPurgeable = true; options.inInputShareable = true; options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap b = BitmapFactory.decodeByteArray(byteArr, 0, count, options); ExifInterface exif = new ExifInterface(IOUtils.getPath(uri, context)); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); b = ImageUtils.cropSquare(b); return rotateBitmap(b, orientation); } catch (Exception e) { e.printStackTrace(); return null; } }