androidx.exifinterface.media.ExifInterface Java Examples

The following examples show how to use androidx.exifinterface.media.ExifInterface. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BitmapUtils.java    From Lassi-Android with MIT License 6 votes vote down vote up
/**
 * Rotate the given image by given Exif value.<br>
 * If no rotation is required the image will not be rotated.<br>
 * New bitmap is created and the old one is recycled.
 */
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
    int degrees;
    int orientation =
            exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    switch (orientation) {
        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;
    }
    return new RotateBitmapResult(bitmap, degrees);
}
 
Example #2
Source File: BitmapUtils.java    From Android-Image-Cropper with Apache License 2.0 6 votes vote down vote up
/**
 * Rotate the given image by given Exif value.<br>
 * If no rotation is required the image will not be rotated.<br>
 * New bitmap is created and the old one is recycled.
 */
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
  int degrees;
  int orientation =
      exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  switch (orientation) {
    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;
  }
  return new RotateBitmapResult(bitmap, degrees);
}
 
Example #3
Source File: BitmapUtil.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static Pair<Integer, Integer> getExifDimensions(InputStream inputStream) throws IOException {
  ExifInterface exif   = new ExifInterface(inputStream);
  int           width  = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
  int           height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
  if (width == 0 && height == 0) {
    return null;
  }

  int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
  if (orientation == ExifInterface.ORIENTATION_ROTATE_90  ||
      orientation == ExifInterface.ORIENTATION_ROTATE_270 ||
      orientation == ExifInterface.ORIENTATION_TRANSVERSE ||
      orientation == ExifInterface.ORIENTATION_TRANSPOSE)
  {
    return new Pair<>(height, width);
  }
  return new Pair<>(width, height);
}
 
Example #4
Source File: BitmapHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
private static int getOrientation(@NonNull final ExifInterface exif) {
	final int rotation = exif.getAttributeInt(
		ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

	int result;
	switch (rotation) {
	case ExifInterface.ORIENTATION_ROTATE_90:
		result = 90;
		break;
	case ExifInterface.ORIENTATION_ROTATE_180:
		result = 180;
		break;
	case ExifInterface.ORIENTATION_ROTATE_270:
		result = 270;
		break;
	case ExifInterface.ORIENTATION_UNDEFINED:
	default:
		result = 0;
		break;
	}
	if (DEBUG) Log.v(TAG, "getOrientation:" + result);
	return result;
}
 
Example #5
Source File: BitmapLoadUtils.java    From EasyPhotos with Apache License 2.0 6 votes vote down vote up
public static int exifToDegrees(int exifOrientation) {
    int rotation;
    switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
        case ExifInterface.ORIENTATION_TRANSPOSE:
            rotation = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            rotation = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
        case ExifInterface.ORIENTATION_TRANSVERSE:
            rotation = 270;
            break;
        default:
            rotation = 0;
    }
    return rotation;
}
 
Example #6
Source File: BitmapLoadUtils.java    From PictureSelector with Apache License 2.0 6 votes vote down vote up
public static int exifToDegrees(int exifOrientation) {
    int rotation;
    switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
        case ExifInterface.ORIENTATION_TRANSPOSE:
            rotation = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            rotation = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
        case ExifInterface.ORIENTATION_TRANSVERSE:
            rotation = 270;
            break;
        default:
            rotation = 0;
    }
    return rotation;
}
 
Example #7
Source File: BitmapUtils.java    From leafpicrevived with GNU General Public License v3.0 6 votes vote down vote up
public static int getOrientation(Uri uri, Context ctx) {

        try (InputStream in = ctx.getContentResolver().openInputStream(uri)) {
            if (in == null) {
                return 0;
            }
            ExifInterface exif = new ExifInterface(in);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return SubsamplingScaleImageView.ORIENTATION_180;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return SubsamplingScaleImageView.ORIENTATION_90;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return SubsamplingScaleImageView.ORIENTATION_270;
                default:
                    return SubsamplingScaleImageView.ORIENTATION_0;
            }
        } catch (IOException e) {
            return 0;
        }
    }
 
Example #8
Source File: BitmapUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static Pair<Integer, Integer> getExifDimensions(InputStream inputStream) throws IOException {
  ExifInterface exif   = new ExifInterface(inputStream);
  int           width  = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
  int           height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
  if (width == 0 || height == 0) {
    return null;
  }

  int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
  if (orientation == ExifInterface.ORIENTATION_ROTATE_90  ||
      orientation == ExifInterface.ORIENTATION_ROTATE_270 ||
      orientation == ExifInterface.ORIENTATION_TRANSVERSE ||
      orientation == ExifInterface.ORIENTATION_TRANSPOSE)
  {
    return new Pair<>(height, width);
  }
  return new Pair<>(width, height);
}
 
Example #9
Source File: AppTools.java    From SSForms with GNU General Public License v3.0 5 votes vote down vote up
public int getCameraPhotoOrientation(Uri imageUri){
    int rotate = 0;
    try {
        File imageFile = new File(imageUri.getPath());

        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }

        Log.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}
 
Example #10
Source File: CropImageView.java    From Android-Image-Cropper with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a Bitmap and initializes the image rotation according to the EXIT data.<br>
 * <br>
 * The EXIF can be retrieved by doing the following: <code>
 * ExifInterface exif = new ExifInterface(path);</code>
 *
 * @param bitmap the original bitmap to set; if null, this
 * @param exif the EXIF information about this bitmap; may be null
 */
public void setImageBitmap(Bitmap bitmap, ExifInterface exif) {
  Bitmap setBitmap;
  int degreesRotated = 0;
  if (bitmap != null && exif != null) {
    BitmapUtils.RotateBitmapResult result = BitmapUtils.rotateBitmapByExif(bitmap, exif);
    setBitmap = result.bitmap;
    degreesRotated = result.degrees;
    mInitialDegreesRotated = result.degrees;
  } else {
    setBitmap = bitmap;
  }
  mCropOverlayView.setInitialCropWindowRect(null);
  setBitmap(setBitmap, 0, null, 1, degreesRotated);
}
 
Example #11
Source File: DrawableProvider.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
/**
 * 读取图片的旋转的角度
 *
 * @param path 图片绝对路径
 * @return 图片的旋转角度
 */
public static int getBitmapDegree(String path) {
    int degree = 0;
    try {
        // 从指定路径下读取图片,并获取其EXIF信息
        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;
            default:
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}
 
Example #12
Source File: BitmapLoadUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
public static int exifToTranslation(int exifOrientation) {
    int translation;
    switch (exifOrientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        case ExifInterface.ORIENTATION_TRANSPOSE:
        case ExifInterface.ORIENTATION_TRANSVERSE:
            translation = -1;
            break;
        default:
            translation = 1;
    }
    return translation;
}
 
Example #13
Source File: BitmapLoadUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
public static int getExifOrientation(@NonNull Context context, @NonNull Uri imageUri) {
    int orientation = ExifInterface.ORIENTATION_UNDEFINED;
    try {
        InputStream stream = context.getContentResolver().openInputStream(imageUri);
        if (stream == null) {
            return orientation;
        }
        orientation = new ImageHeaderParser(stream).getOrientation();
        close(stream);
    } catch (IOException e) {
        Log.e(TAG, "getExifOrientation: " + imageUri.toString(), e);
    }
    return orientation;
}
 
Example #14
Source File: Media.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
@Deprecated
public boolean setOrientation(final int orientation) {
    this.orientation = orientation;
    // TODO: 28/08/16  find a better way
    // TODO update also content provider
    new Thread(new Runnable() {
        public void run() {
            int exifOrientation = -1;
            try {
                ExifInterface exif = new ExifInterface(path);
                switch (orientation) {
                    case 90:
                        exifOrientation = ExifInterface.ORIENTATION_ROTATE_90;
                        break;
                    case 180:
                        exifOrientation = ExifInterface.ORIENTATION_ROTATE_180;
                        break;
                    case 270:
                        exifOrientation = ExifInterface.ORIENTATION_ROTATE_270;
                        break;
                    case 0:
                        exifOrientation = ExifInterface.ORIENTATION_NORMAL;
                        break;
                }
                if (exifOrientation != -1) {
                    exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(exifOrientation));
                    exif.saveAttributes();
                }
            } catch (IOException ignored) {
            }
        }
    }).start();
    return true;
}
 
Example #15
Source File: BitmapLoadUtils.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
public static int exifToTranslation(int exifOrientation) {
    int translation;
    switch (exifOrientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        case ExifInterface.ORIENTATION_TRANSPOSE:
        case ExifInterface.ORIENTATION_TRANSVERSE:
            translation = -1;
            break;
        default:
            translation = 1;
    }
    return translation;
}
 
Example #16
Source File: BitmapLoadUtils.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
public static int getExifOrientation(@NonNull Context context, @NonNull Uri imageUri) {
    int orientation = ExifInterface.ORIENTATION_UNDEFINED;
    try {
        InputStream stream = context.getContentResolver().openInputStream(imageUri);
        if (stream == null) {
            return orientation;
        }
        orientation = new ImageHeaderParser(stream).getOrientation();
        close(stream);
    } catch (IOException e) {
        Log.e(TAG, "getExifOrientation: " + imageUri.toString(), e);
    }
    return orientation;
}
 
Example #17
Source File: CropImageView.java    From Lassi-Android with MIT License 5 votes vote down vote up
/**
 * Sets a Bitmap and initializes the image rotation according to the EXIT data.<br>
 * <br>
 * The EXIF can be retrieved by doing the following: <code>
 * ExifInterface exif = new ExifInterface(path);</code>
 *
 * @param bitmap the original bitmap to set; if null, this
 * @param exif   the EXIF information about this bitmap; may be null
 */
public void setImageBitmap(Bitmap bitmap, ExifInterface exif) {
    Bitmap setBitmap;
    int degreesRotated = 0;
    if (bitmap != null && exif != null) {
        BitmapUtils.RotateBitmapResult result = BitmapUtils.rotateBitmapByExif(bitmap, exif);
        setBitmap = result.bitmap;
        degreesRotated = result.degrees;
        mInitialDegreesRotated = result.degrees;
    } else {
        setBitmap = bitmap;
    }
    mCropOverlayView.setInitialCropWindowRect(null);
    setBitmap(setBitmap, 0, null, 1, degreesRotated);
}
 
Example #18
Source File: CameraUtils.java    From Lassi-Android with MIT License 5 votes vote down vote up
public static int readExifOrientation(int exifOrientation) {
    int orientation;
    switch (exifOrientation) {
        case ExifInterface.ORIENTATION_NORMAL:
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            orientation = 0;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            orientation = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_90:
        case ExifInterface.ORIENTATION_TRANSPOSE:
            orientation = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
        case ExifInterface.ORIENTATION_TRANSVERSE:
            orientation = 270;
            break;

        default:
            orientation = 0;
    }
    return orientation;
}
 
Example #19
Source File: FullPictureRecorder.java    From Lassi-Android with MIT License 5 votes vote down vote up
@Override
void take() {
    mCamera.takePicture(
            new Camera.ShutterCallback() {
                @Override
                public void onShutter() {
                    dispatchOnShutter(true);
                }
            },
            null,
            null,
            new Camera.PictureCallback() {
                @Override
                public void onPictureTaken(byte[] data, final Camera camera) {
                    int exifRotation;
                    try {
                        ExifInterface exif = new ExifInterface(new ByteArrayInputStream(data));
                        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                        exifRotation = CameraUtils.readExifOrientation(exifOrientation);
                    } catch (IOException e) {
                        exifRotation = 0;
                    }
                    mResult.format = PictureResult.FORMAT_JPEG;
                    mResult.data = data;
                    mResult.rotation = exifRotation;
                    camera.startPreview(); // This is needed, read somewhere in the docs.
                    dispatchResult();
                }
            }
    );
}
 
Example #20
Source File: ImageViewFragment.java    From tindroid with Apache License 2.0 4 votes vote down vote up
@SuppressLint("SetTextI18n")
@Override
public void onResume() {
    super.onResume();

    Activity activity = getActivity();
    Bundle args = getArguments();
    if (activity == null || args == null) {
        return;
    }

    mMatrix.reset();

    Bitmap bmp = null;
    byte[] bits = args.getByteArray(AttachmentHandler.ARG_SRC_BYTES);
    if (bits != null) {
        bmp = BitmapFactory.decodeByteArray(bits, 0, bits.length);
    } else {
        Uri uri = args.getParcelable(AttachmentHandler.ARG_SRC_URI);
        if (uri != null) {
            final ContentResolver resolver = activity.getContentResolver();
            // Resize image to ensure it's under the maximum in-band size.
            try {
                InputStream is = resolver.openInputStream(uri);
                if (is != null) {
                    bmp = BitmapFactory.decodeStream(is, null, null);
                    is.close();
                }
                // Make sure the bitmap is properly oriented in preview.
                is = resolver.openInputStream(uri);
                if (is != null) {
                    ExifInterface exif = new ExifInterface(is);
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);
                    if (bmp != null) {
                        bmp = UiUtils.rotateBitmap(bmp, orientation);
                    }
                    is.close();
                }
            } catch (IOException ex) {
                Log.i(TAG, "Failed to read image from " + uri, ex);
            }
        }
    }

    if (bmp != null) {
        String filename = args.getString(AttachmentHandler.ARG_FILE_NAME);
        if (TextUtils.isEmpty(filename)) {
            filename = getResources().getString(R.string.tinode_image);
        }

        activity.findViewById(R.id.metaPanel).setVisibility(View.VISIBLE);

        mInitialRect = new RectF(0, 0, bmp.getWidth(), bmp.getHeight());
        mWorkingRect = new RectF(mInitialRect);
        if (bits == null) {
            // The image is being previewed before sending.
            activity.findViewById(R.id.sendImagePanel).setVisibility(View.VISIBLE);
            activity.findViewById(R.id.annotation).setVisibility(View.GONE);
            setHasOptionsMenu(false);
        } else {
            // The received image is viewed.
            String size = ((int) mInitialRect.width()) + " \u00D7 " + ((int) mInitialRect.height()) + "; ";
            activity.findViewById(R.id.sendImagePanel).setVisibility(View.GONE);
            activity.findViewById(R.id.annotation).setVisibility(View.VISIBLE);
            ((TextView) activity.findViewById(R.id.content_type)).setText(args.getString("mime"));
            ((TextView) activity.findViewById(R.id.file_name)).setText(filename);
            ((TextView) activity.findViewById(R.id.image_size)).setText(size + UiUtils.bytesToHumanSize(bits.length));
            setHasOptionsMenu(true);
        }

        mImageView.setImageDrawable(new BitmapDrawable(getResources(), bmp));

        // ImageView size is set later. Must add an observer to get the size.
        mImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ensure we call it only once.
                mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mScreenRect = new RectF(0, 0, mImageView.getWidth(), mImageView.getHeight());
                mMatrix.setRectToRect(mInitialRect, mScreenRect, Matrix.ScaleToFit.CENTER);
                mWorkingMatrix = new Matrix(mMatrix);

                mImageView.setImageMatrix(mMatrix);
            }
        });
    } else {
        // Show broken image.
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mImageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_broken_image));
        activity.findViewById(R.id.metaPanel).setVisibility(View.INVISIBLE);

        setHasOptionsMenu(false);
    }
}
 
Example #21
Source File: ImageUpdater.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 13) {
            PhotoViewer.getInstance().setParentActivity(parentFragment.getParentActivity());
            int orientation = 0;
            try {
                ExifInterface ei = new ExifInterface(currentPicturePath);
                int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (exif) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        orientation = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        orientation = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        orientation = 270;
                        break;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            final ArrayList<Object> arrayList = new ArrayList<>();
            arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false, 0, 0, 0));
            PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, PhotoViewer.SELECT_TYPE_AVATAR, false, new PhotoViewer.EmptyPhotoViewerProvider() {
                @Override
                public void sendButtonPressed(int index, VideoEditedInfo videoEditedInfo, boolean notify, int scheduleDate) {
                    String path = null;
                    MediaController.PhotoEntry photoEntry = (MediaController.PhotoEntry) arrayList.get(0);
                    if (photoEntry.imagePath != null) {
                        path = photoEntry.imagePath;
                    } else if (photoEntry.path != null) {
                        path = photoEntry.path;
                    }
                    Bitmap bitmap = ImageLoader.loadBitmap(path, null, 800, 800, true);
                    processBitmap(bitmap);
                }

                @Override
                public boolean allowCaption() {
                    return false;
                }

                @Override
                public boolean canScrollAway() {
                    return false;
                }
            }, null);
            AndroidUtilities.addMediaToGallery(currentPicturePath);
            currentPicturePath = null;
        } else if (requestCode == 14) {
            if (data == null || data.getData() == null) {
                return;
            }
            startCrop(null, data.getData());
        }
    }
}
 
Example #22
Source File: Image.java    From Field-Book with GNU General Public License v2.0 4 votes vote down vote up
public void loadImage() {
    Bitmap bitmap;
    if (!file.exists()) {
        bitmap = missing;
        width = missing.getWidth();
        height = missing.getHeight();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, stream);
        bytes = stream.toByteArray();
        fileSize = bytes.length;
    } else {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), bmOptions);
        width = bmOptions.outWidth;
        height = bmOptions.outHeight;
        bytes = new byte[(int) file.length()];

        try {
            ExifInterface exif = new ExifInterface(file.getAbsolutePath());
            double latlon[] = exif.getLatLong();
            if (latlon != null) {
                double lat = latlon[0];
                double lon = latlon[1];
                location.setType(GeoJSON.TypeEnum.FEATURE);
                JsonObject o = new JsonObject();
                o.addProperty("type", "Point");
                JsonArray a = new JsonArray();
                a.add(lon);
                a.add(lat);
                o.add("coordinates", a);
                location.setGeometry(o);
            }

            FileInputStream fin = new FileInputStream(file);
            fin.read(bytes);
        } catch (IOException e) {
        }
    }

    mimeType = "image/jpeg";
}
 
Example #23
Source File: ImageUpdater.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 13) {
            PhotoViewer.getInstance().setParentActivity(parentFragment.getParentActivity());
            int orientation = 0;
            try {
                ExifInterface ei = new ExifInterface(currentPicturePath);
                int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (exif) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        orientation = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        orientation = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        orientation = 270;
                        break;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            final ArrayList<Object> arrayList = new ArrayList<>();
            arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false, 0, 0, 0));
            PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, PhotoViewer.SELECT_TYPE_AVATAR, false, new PhotoViewer.EmptyPhotoViewerProvider() {
                @Override
                public void sendButtonPressed(int index, VideoEditedInfo videoEditedInfo, boolean notify, int scheduleDate) {
                    String path = null;
                    MediaController.PhotoEntry photoEntry = (MediaController.PhotoEntry) arrayList.get(0);
                    if (photoEntry.imagePath != null) {
                        path = photoEntry.imagePath;
                    } else if (photoEntry.path != null) {
                        path = photoEntry.path;
                    }
                    Bitmap bitmap = ImageLoader.loadBitmap(path, null, 800, 800, true);
                    processBitmap(bitmap);
                }

                @Override
                public boolean allowCaption() {
                    return false;
                }

                @Override
                public boolean canScrollAway() {
                    return false;
                }
            }, null);
            AndroidUtilities.addMediaToGallery(currentPicturePath);
            currentPicturePath = null;
        } else if (requestCode == 14) {
            if (data == null || data.getData() == null) {
                return;
            }
            startCrop(null, data.getData());
        }
    }
}
 
Example #24
Source File: CameraActivity.java    From cordova-plugin-camera-preview with MIT License 4 votes vote down vote up
private static int exifToDegrees(int exifOrientation) {
  if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
  else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; }
  else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }
  return 0;
}
 
Example #25
Source File: ImageHeaderParser.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
public static void copyExif(ExifInterface originalExif, int width, int height, String imageOutputPath) {
    String[] attributes = new String[]{
            ExifInterface.TAG_F_NUMBER,
            ExifInterface.TAG_DATETIME,
            ExifInterface.TAG_DATETIME_DIGITIZED,
            ExifInterface.TAG_EXPOSURE_TIME,
            ExifInterface.TAG_FLASH,
            ExifInterface.TAG_FOCAL_LENGTH,
            ExifInterface.TAG_GPS_ALTITUDE,
            ExifInterface.TAG_GPS_ALTITUDE_REF,
            ExifInterface.TAG_GPS_DATESTAMP,
            ExifInterface.TAG_GPS_LATITUDE,
            ExifInterface.TAG_GPS_LATITUDE_REF,
            ExifInterface.TAG_GPS_LONGITUDE,
            ExifInterface.TAG_GPS_LONGITUDE_REF,
            ExifInterface.TAG_GPS_PROCESSING_METHOD,
            ExifInterface.TAG_GPS_TIMESTAMP,
            ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY,
            ExifInterface.TAG_MAKE,
            ExifInterface.TAG_MODEL,
            ExifInterface.TAG_SUBSEC_TIME,
            ExifInterface.TAG_SUBSEC_TIME_DIGITIZED,
            ExifInterface.TAG_SUBSEC_TIME_ORIGINAL,
            ExifInterface.TAG_WHITE_BALANCE
    };

    try {
        ExifInterface newExif = new ExifInterface(imageOutputPath);
        String value;
        for (String attribute : attributes) {
            value = originalExif.getAttribute(attribute);
            if (!TextUtils.isEmpty(value)) {
                newExif.setAttribute(attribute, value);
            }
        }
        newExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, String.valueOf(width));
        newExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, String.valueOf(height));
        newExif.setAttribute(ExifInterface.TAG_ORIENTATION, "0");

        newExif.saveAttributes();

    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
}
 
Example #26
Source File: PhotoEditorActivity.java    From react-native-photo-editor with Apache License 2.0 4 votes vote down vote up
private void returnBackWithUpdateImage() {
        updateView(View.GONE);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        parentImageRelativeLayout.setLayoutParams(layoutParams);
        new CountDownTimer(1000, 500) {
            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                String imageName = "/IMG_" + timeStamp + ".jpg";

                 String selectedImagePath = getIntent().getExtras().getString("selectedImagePath");
                 File file = new File(selectedImagePath);
//                String newPath = getCacheDir() + imageName;
//	            File file = new File(newPath);

                try {
                    FileOutputStream out = new FileOutputStream(file);
                    if (parentImageRelativeLayout != null) {
                        parentImageRelativeLayout.setDrawingCacheEnabled(true);
                        Bitmap bitmap = parentImageRelativeLayout.getDrawingCache();
                        Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true);
                        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
                    }

                    out.flush();
                    out.close();
                    try {
                        ExifInterface exifDest = new ExifInterface(file.getAbsolutePath());
                        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(imageOrientation));
                        exifDest.saveAttributes();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception var7) {
                    var7.printStackTrace();
                }

                Intent returnIntent = new Intent();
                returnIntent.putExtra("imagePath", selectedImagePath);
                setResult(Activity.RESULT_OK, returnIntent);

                finish();
            }
        }.start();
    }
 
Example #27
Source File: UiUtils.java    From tindroid with Apache License 2.0 4 votes vote down vote up
@NonNull
static Bitmap rotateBitmap(@NonNull Bitmap bmp, int orientation) {
    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bmp;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bmp;
    }

    try {
        Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
        bmp.recycle();
        return rotated;
    } catch (OutOfMemoryError ex) {
        Log.e(TAG, "Out of memory while rotating bitmap");
        return bmp;
    }
}
 
Example #28
Source File: ImageHelper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static Matrix getImageRotation(File file) {
    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return null;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                return matrix;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                return matrix;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                return matrix;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                return matrix;
            default:
                return null;
        }
    } catch (Throwable ex /* IOException */) {
        /*
            java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000
            java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000
              at android.media.MediaMetadataRetriever._setDataSource(Native Method)
              at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:226)
              at androidx.exifinterface.media.ExifInterface.getHeifAttributes(SourceFile:5716)
              at androidx.exifinterface.media.ExifInterface.loadAttributes(SourceFile:4556)
              at androidx.exifinterface.media.ExifInterface.initForFilename(SourceFile:5195)
              at androidx.exifinterface.media.ExifInterface.<init>(SourceFile:3926)
         */
        Log.w(ex);
        return null;
    }
}
 
Example #29
Source File: ImageHeaderParser.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
public static void copyExif(ExifInterface originalExif, int width, int height, String imageOutputPath) {
    String[] attributes = new String[]{
            ExifInterface.TAG_F_NUMBER,
            ExifInterface.TAG_DATETIME,
            ExifInterface.TAG_DATETIME_DIGITIZED,
            ExifInterface.TAG_EXPOSURE_TIME,
            ExifInterface.TAG_FLASH,
            ExifInterface.TAG_FOCAL_LENGTH,
            ExifInterface.TAG_GPS_ALTITUDE,
            ExifInterface.TAG_GPS_ALTITUDE_REF,
            ExifInterface.TAG_GPS_DATESTAMP,
            ExifInterface.TAG_GPS_LATITUDE,
            ExifInterface.TAG_GPS_LATITUDE_REF,
            ExifInterface.TAG_GPS_LONGITUDE,
            ExifInterface.TAG_GPS_LONGITUDE_REF,
            ExifInterface.TAG_GPS_PROCESSING_METHOD,
            ExifInterface.TAG_GPS_TIMESTAMP,
            ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY,
            ExifInterface.TAG_MAKE,
            ExifInterface.TAG_MODEL,
            ExifInterface.TAG_SUBSEC_TIME,
            ExifInterface.TAG_SUBSEC_TIME_DIGITIZED,
            ExifInterface.TAG_SUBSEC_TIME_ORIGINAL,
            ExifInterface.TAG_WHITE_BALANCE
    };

    try {
        ExifInterface newExif = new ExifInterface(imageOutputPath);
        String value;
        for (String attribute : attributes) {
            value = originalExif.getAttribute(attribute);
            if (!TextUtils.isEmpty(value)) {
                newExif.setAttribute(attribute, value);
            }
        }
        newExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, String.valueOf(width));
        newExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, String.valueOf(height));
        newExif.setAttribute(ExifInterface.TAG_ORIENTATION, "0");

        newExif.saveAttributes();

    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
}
 
Example #30
Source File: PhotoEditorActivity.java    From react-native-photo-editor with Apache License 2.0 4 votes vote down vote up
private void returnBackWithSavedImage() {
    int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        updateView(View.GONE);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        parentImageRelativeLayout.setLayoutParams(layoutParams);
        new CountDownTimer(1000, 500) {
            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                String imageName = "IMG_" + timeStamp + ".jpg";
                Intent returnIntent = new Intent();

                if (isSDCARDMounted()) {
                    String folderName = "PhotoEditorSDK";
                    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName);
                    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
                        Log.d("PhotoEditorSDK", "Failed to create directory");
                    }

                    String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
                    returnIntent.putExtra("imagePath", selectedOutputPath);
                    Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath);
                    File file = new File(selectedOutputPath);

                    try {
                        FileOutputStream out = new FileOutputStream(file);
                        if (parentImageRelativeLayout != null) {
                            parentImageRelativeLayout.setDrawingCacheEnabled(true);

                            Bitmap bitmap = parentImageRelativeLayout.getDrawingCache();
                            Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true);
                            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
                        }

                        out.flush();
                        out.close();

                        try {
                            ExifInterface exifDest = new ExifInterface(file.getAbsolutePath());
                            exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(imageOrientation));
                            exifDest.saveAttributes();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (Exception var7) {
                        var7.printStackTrace();
                    }
                }

                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        }.start();
        Toast.makeText(this, getString(R.string.save_image_succeed), Toast.LENGTH_SHORT).show();
    } else {
        showPermissionRequest();
    }
}