Search in sources :

Example 81 with ExifInterface

use of android.media.ExifInterface in project react-native-image-picker by marcshilling.

the class MediaUtils method readExifInterface.

public static ReadExifResult readExifInterface(@NonNull ResponseHelper responseHelper, @NonNull final ImageConfig imageConfig) {
    ReadExifResult result;
    int currentRotation = 0;
    try {
        ExifInterface exif = new ExifInterface(imageConfig.original.getAbsolutePath());
        // extract lat, long, and timestamp and add to the response
        float[] latlng = new float[2];
        exif.getLatLong(latlng);
        float latitude = latlng[0];
        float longitude = latlng[1];
        if (latitude != 0f || longitude != 0f) {
            responseHelper.putDouble("latitude", latitude);
            responseHelper.putDouble("longitude", longitude);
        }
        final String timestamp = exif.getAttribute(ExifInterface.TAG_DATETIME);
        final SimpleDateFormat exifDatetimeFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        final DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            final String isoFormatString = new StringBuilder(isoFormat.format(exifDatetimeFormat.parse(timestamp))).append("Z").toString();
            responseHelper.putString("timestamp", isoFormatString);
        } catch (Exception e) {
        }
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        boolean isVertical = true;
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                isVertical = false;
                currentRotation = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                isVertical = false;
                currentRotation = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                currentRotation = 180;
                break;
        }
        responseHelper.putInt("originalRotation", currentRotation);
        responseHelper.putBoolean("isVertical", isVertical);
        result = new ReadExifResult(currentRotation, null);
    } catch (IOException e) {
        e.printStackTrace();
        result = new ReadExifResult(currentRotation, e);
    }
    return result;
}
Also used : ExifInterface(android.media.ExifInterface) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 82 with ExifInterface

use of android.media.ExifInterface in project Talon-for-Twitter by klinker24.

the class DirectMessageConversation method getThumbnail.

private Bitmap getThumbnail(Uri uri) throws IOException {
    InputStream input = getContentResolver().openInputStream(uri);
    int reqWidth = 150;
    int reqHeight = 150;
    byte[] byteArr = new byte[0];
    byte[] buffer = new byte[1024];
    int len;
    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 = Compose.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);
        if (!Compose.isAndroidN()) {
            ExifInterface exif = new ExifInterface(IOUtils.getPath(uri, context));
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            input.close();
            b = ImageUtils.cropSquare(b);
            return Compose.rotateBitmap(b, orientation);
        } else {
            input.close();
            b = ImageUtils.cropSquare(b);
            return b;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) BitmapFactory(android.graphics.BitmapFactory) Point(android.graphics.Point) TwitterException(twitter4j.TwitterException) IOException(java.io.IOException)

Example 83 with ExifInterface

use of android.media.ExifInterface in project Talon-for-Twitter by klinker24.

the class Compose method getThumbnail.

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;
    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;
    }
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) BitmapFactory(android.graphics.BitmapFactory) Point(android.graphics.Point) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 84 with ExifInterface

use of android.media.ExifInterface in project Talon-for-Twitter by klinker24.

the class ProfilePager method getBitmapToSend.

private Bitmap getBitmapToSend(Uri uri) throws FileNotFoundException, IOException {
    InputStream input = getContentResolver().openInputStream(uri);
    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    // optional
    onlyBoundsOptions.inDither = true;
    // optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();
    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;
    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
    double ratio = (originalSize > 1000) ? (originalSize / 1000) : 1.0;
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    // optional
    bitmapOptions.inDither = true;
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    input = this.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    ExifInterface exif = new ExifInterface(IOUtils.getPath(uri, context));
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    input.close();
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) BitmapFactory(android.graphics.BitmapFactory) Point(android.graphics.Point)

Example 85 with ExifInterface

use of android.media.ExifInterface in project ARCamera by escnqh.

the class ShotActivity method try2Shot.

public void try2Shot(Mat mRgbaT, Mat mGrayT) {
    final long currentTimeMillis = System.currentTimeMillis();
    if (currentTimeMillis - nowtime > 1000 && FLAG < 21) {
        Log.i("Time Logggggggggg", currentTimeMillis + "");
        nowtime = currentTimeMillis;
        FLAG++;
        Mat mBgr = new Mat();
        final String photoPath = albumPath + File.separator + currentTimeMillis + ".jpg";
        final ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, photoPath);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
        values.put(MediaStore.Images.Media.TITLE, appName);
        values.put(MediaStore.Images.Media.DESCRIPTION, appName);
        values.put(MediaStore.Images.Media.DATE_TAKEN, currentTimeMillis);
        // Ensure that the album directory exists.
        File album = new File(albumPath);
        if (!album.isDirectory() && !album.mkdirs()) {
            Log.e(TAG, "Failed to create album directory at " + albumPath);
        }
        // Try to create the photo.
        Imgproc.cvtColor(mRgbaT, mBgr, Imgproc.COLOR_RGBA2BGR, 3);
        if (!Imgcodecs.imwrite(photoPath, mBgr)) {
            Log.e(TAG, "Failed to save photo to " + photoPath);
        }
        Log.d(TAG, "Photo saved successfully to " + photoPath);
        // Try to insert the photo into the MediaStore.
        Uri uri;
        try {
            uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } catch (final Exception e) {
            Log.e(TAG, "Failed to insert photo into MediaStore");
            e.printStackTrace();
            // Since the insertion failed, delete the photo.
            File photo = new File(photoPath);
            if (!photo.delete()) {
                Log.e(TAG, "Failed to delete non-inserted photo");
            }
        }
        try {
            ExifInterface exif = new ExifInterface(photoPath);
            exif.setAttribute(ExifInterface.TAG_MAKE, "ShotByPeelson");
            exif.setAttribute(ExifInterface.TAG_MODEL, "ShotByPeelson");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : ContentValues(android.content.ContentValues) Mat(org.opencv.core.Mat) ExifInterface(android.media.ExifInterface) IOException(java.io.IOException) File(java.io.File) Uri(android.net.Uri) IOException(java.io.IOException)

Aggregations

ExifInterface (android.media.ExifInterface)153 IOException (java.io.IOException)93 Paint (android.graphics.Paint)32 Bitmap (android.graphics.Bitmap)26 InputStream (java.io.InputStream)21 File (java.io.File)17 BitmapFactory (android.graphics.BitmapFactory)16 Matrix (android.graphics.Matrix)15 FileInputStream (java.io.FileInputStream)15 Point (android.graphics.Point)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ErrnoException (android.system.ErrnoException)10 BufferedInputStream (java.io.BufferedInputStream)10 FileDescriptor (java.io.FileDescriptor)10 FileNotFoundException (java.io.FileNotFoundException)10 SuppressLint (android.annotation.SuppressLint)9 FileOutputStream (java.io.FileOutputStream)8 Cursor (android.database.Cursor)7 AssetFileDescriptor (android.content.res.AssetFileDescriptor)6 Bundle (android.os.Bundle)5