Example usage for android.graphics Matrix Matrix

List of usage examples for android.graphics Matrix Matrix

Introduction

In this page you can find the example usage for android.graphics Matrix Matrix.

Prototype

public Matrix() 

Source Link

Document

Create an identity matrix

Usage

From source file:com.irccloud.android.activity.MainActivity.java

private Uri resize(Uri in) {
    Uri out = null;//from w  ww. ja  va2  s .  c om
    try {
        int MAX_IMAGE_SIZE = Integer
                .parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString("photo_size", "1024"));
        File imageDir = new File(Environment.getExternalStorageDirectory(), "IRCCloud");
        imageDir.mkdirs();
        new File(imageDir, ".nomedia").createNewFile();

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(IRCCloudApplication.getInstance().getApplicationContext()
                .getContentResolver().openInputStream(in), null, o);
        int scale = 1;

        if (o.outWidth < MAX_IMAGE_SIZE && o.outHeight < MAX_IMAGE_SIZE)
            return in;

        if (o.outWidth > o.outHeight) {
            if (o.outWidth > MAX_IMAGE_SIZE)
                scale = o.outWidth / MAX_IMAGE_SIZE;
        } else {
            if (o.outHeight > MAX_IMAGE_SIZE)
                scale = o.outHeight / MAX_IMAGE_SIZE;
        }

        o = new BitmapFactory.Options();
        o.inSampleSize = scale;
        Bitmap bmp = BitmapFactory.decodeStream(IRCCloudApplication.getInstance().getApplicationContext()
                .getContentResolver().openInputStream(in), null, o);

        //ExifInterface can only work on local files, so make a temporary copy on the SD card
        out = Uri.fromFile(File.createTempFile("irccloudcapture-original", ".jpg", imageDir));
        InputStream is = IRCCloudApplication.getInstance().getApplicationContext().getContentResolver()
                .openInputStream(in);
        OutputStream os = IRCCloudApplication.getInstance().getApplicationContext().getContentResolver()
                .openOutputStream(out);
        byte[] buffer = new byte[8192];
        int len;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        os.close();

        ExifInterface exif = new ExifInterface(out.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        new File(new URI(out.toString())).delete();

        out = Uri.fromFile(File.createTempFile("irccloudcapture-resized", ".jpg", imageDir));
        if (orientation > 1) {
            Matrix matrix = new Matrix();
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.postRotate(90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.postRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.postRotate(270);
                break;
            }
            try {
                Bitmap oldbmp = bmp;
                bmp = Bitmap.createBitmap(oldbmp, 0, 0, oldbmp.getWidth(), oldbmp.getHeight(), matrix, true);
                oldbmp.recycle();
            } catch (OutOfMemoryError e) {
                Log.e("IRCCloud", "Out of memory rotating the photo, it may look wrong on imgur");
            }
        }

        if (bmp == null || !bmp.compress(android.graphics.Bitmap.CompressFormat.JPEG, 90, IRCCloudApplication
                .getInstance().getApplicationContext().getContentResolver().openOutputStream(out))) {
            out = null;
        }
        if (bmp != null)
            bmp.recycle();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        Crashlytics.logException(e);
    } catch (OutOfMemoryError e) {
        Log.e("IRCCloud", "Out of memory rotating the photo, it may look wrong on imgur");
    }
    if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("keep_photos", false)
            && in.toString().contains("irccloudcapture")) {
        try {
            new File(new URI(in.toString())).delete();
        } catch (Exception e) {
        }
    }
    if (out != null)
        return out;
    else
        return in;
}