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.fabernovel.alertevoirie.ReportDetailsActivity.java

private void RotatePicture(ImageView imageView) {
    Bitmap picture = null;//from  w ww.  j  a v  a 2  s  .c o  m

    picture = ((BitmapDrawable) (imageView.getDrawable())).getBitmap();

    Matrix m = new Matrix();
    m.postRotate(90);
    picture = Bitmap.createBitmap(picture, 0, 0, picture.getWidth(), picture.getHeight(), m, true);

    imageView.setImageBitmap(picture);

}

From source file:fr.bde_eseo.eseomega.GantierActivity.java

public Bitmap getResizedBitmap(Bitmap bm, int newSize) {
    int width = bm.getWidth();
    int height = bm.getHeight();

    float scale = ((float) newSize) / width;

    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scale, scale);//from ww  w  .  j av a 2 s .  c  o m

    // "RECREATE" THE NEW BITMAP
    return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}

From source file:fr.bde_eseo.eseomega.GantierActivity.java

public Bitmap getScaledResizedBitmap(Bitmap bm, int newSize) {
    int width = bm.getWidth();
    int height = bm.getHeight();

    float scale;//from www.j  av  a2s.  c  om

    if (height > width) { // portrait bitmap
        scale = ((float) newSize) / height;
    } else { // landscape bitmap
        scale = ((float) newSize) / width;
    }

    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scale, scale);

    // "RECREATE" THE NEW BITMAP
    return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}

From source file:com.annanovas.bestprice.DashBoardEditActivity.java

private Bitmap imageProcess(Bitmap imageBitmap) {
    int width = imageBitmap.getWidth();
    int height = imageBitmap.getHeight();
    int newWidth = 1000;
    int newHeight = 1000;
    if (width > 1000) {
        double x = (double) width / 1000d;
        newHeight = (int) (height / x);
        newWidth = 1000;/*from   w w w .j a  v a2s . c  om*/
    } else if (height > 1000) {
        double x = (double) height / 1000d;
        newWidth = (int) (width / x);
        newHeight = 1000;
    }
    // calculate the scale - in this case = 0.4f
    ExifInterface exif = null;
    int rotationAngle = 0;
    try {
        exif = new ExifInterface(imageUri);
        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        // showLog("orientString:" + orientString + " DateTime:" + exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));
        int orientation = orientString != null ? Integer.parseInt(orientString)
                : ExifInterface.ORIENTATION_NORMAL;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
            rotationAngle = 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
            rotationAngle = 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
            rotationAngle = 270;
        //  showLog("Rotation Angle:" + rotationAngle);
    } catch (IOException e) {
        // showLog("ExifInterface Failed!");
        e.printStackTrace();
    }
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    matrix.postRotate(rotationAngle);
    return Bitmap.createBitmap(imageBitmap, 0, 0, width, height, matrix, true);
}

From source file:com.mibr.android.intelligentreminder.INeedToo.java

public static BitmapDrawable scaleTo(BitmapDrawable bmd, float newSize) {
    Bitmap bm = bmd.getBitmap();//from   w w  w  .j  a  va 2 s  .co  m
    int width = bm.getWidth();
    float scale = (float) (newSize / (float) width);
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    Bitmap bmNew = Bitmap.createBitmap(bm, 0, 0, width, width, matrix, true);
    return new BitmapDrawable(bmNew);
}

From source file:com.almalence.opencam.SavingService.java

protected void addTimestamp(File file, int exif_orientation) {
    try {//from  w  w w .j  av  a  2s.  c om
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        int dateFormat = Integer.parseInt(prefs.getString(ApplicationScreen.sTimestampDate, "0"));
        boolean abbreviation = prefs.getBoolean(ApplicationScreen.sTimestampAbbreviation, false);
        int saveGeo = Integer.parseInt(prefs.getString(ApplicationScreen.sTimestampGeo, "0"));
        int timeFormat = Integer.parseInt(prefs.getString(ApplicationScreen.sTimestampTime, "0"));
        int separator = Integer.parseInt(prefs.getString(ApplicationScreen.sTimestampSeparator, "0"));
        String customText = prefs.getString(ApplicationScreen.sTimestampCustomText, "");
        int color = Integer.parseInt(prefs.getString(ApplicationScreen.sTimestampColor, "1"));
        int fontSizeC = Integer.parseInt(prefs.getString(ApplicationScreen.sTimestampFontSize, "80"));

        String formattedCurrentDate = "";
        if (dateFormat == 0 && timeFormat == 0 && customText.equals("") && saveGeo == 0)
            return;

        String geoText = "";
        // show geo data on time stamp
        if (saveGeo != 0) {
            Location l = MLocation.getLocation(getApplicationContext());

            if (l != null) {
                if (saveGeo == 2) {
                    Geocoder geocoder = new Geocoder(MainScreen.getMainContext(), Locale.getDefault());
                    List<Address> list = geocoder.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
                    if (!list.isEmpty()) {
                        String country = list.get(0).getCountryName();
                        String locality = list.get(0).getLocality();
                        String adminArea = list.get(0).getSubAdminArea();// city
                        // localized
                        String street = list.get(0).getThoroughfare();// street
                        // localized
                        String address = list.get(0).getAddressLine(0);

                        // replace street and city with localized name
                        if (street != null)
                            address = street;
                        if (adminArea != null)
                            locality = adminArea;

                        geoText = (country != null ? country : "") + (locality != null ? (", " + locality) : "")
                                + (address != null ? (", \n" + address) : "");

                        if (geoText.equals(""))
                            geoText = "lat:" + l.getLatitude() + "\nlng:" + l.getLongitude();
                    }
                } else
                    geoText = "lat:" + l.getLatitude() + "\nlng:" + l.getLongitude();
            }
        }

        String dateFormatString = "";
        String timeFormatString = "";
        String separatorString = ".";
        String monthString = abbreviation ? "MMMM" : "MM";

        switch (separator) {
        case 0:
            separatorString = "/";
            break;
        case 1:
            separatorString = ".";
            break;
        case 2:
            separatorString = "-";
            break;
        case 3:
            separatorString = " ";
            break;
        default:
            separatorString = " ";
        }

        switch (dateFormat) {
        case 1:
            dateFormatString = "yyyy" + separatorString + monthString + separatorString + "dd";
            break;
        case 2:
            dateFormatString = "dd" + separatorString + monthString + separatorString + "yyyy";
            break;
        case 3:
            dateFormatString = monthString + separatorString + "dd" + separatorString + "yyyy";
            break;
        default:
        }

        switch (timeFormat) {
        case 1:
            timeFormatString = " hh:mm:ss a";
            break;
        case 2:
            timeFormatString = " HH:mm:ss";
            break;
        default:
        }

        Date currentDate = Calendar.getInstance().getTime();
        java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
                dateFormatString + timeFormatString);
        formattedCurrentDate = simpleDateFormat.format(currentDate);

        formattedCurrentDate += (customText.isEmpty() ? "" : ("\n" + customText))
                + (geoText.isEmpty() ? "" : ("\n" + geoText));

        if (formattedCurrentDate.equals(""))
            return;

        Bitmap sourceBitmap;
        Bitmap bitmap;

        int rotation = 0;
        Matrix matrix = new Matrix();
        if (exif_orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            rotation = 90;
        } else if (exif_orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            rotation = 180;
        } else if (exif_orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            rotation = 270;
        }
        matrix.postRotate(rotation);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inMutable = true;

        sourceBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        bitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(),
                matrix, false);

        sourceBitmap.recycle();

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Paint p = new Paint();

        Canvas canvas = new Canvas(bitmap);

        final float scale = getResources().getDisplayMetrics().density;

        p.setColor(Color.WHITE);
        switch (color) {
        case 0:
            color = Color.BLACK;
            p.setColor(Color.BLACK);
            break;
        case 1:
            color = Color.WHITE;
            p.setColor(Color.WHITE);
            break;
        case 2:
            color = Color.YELLOW;
            p.setColor(Color.YELLOW);
            break;

        }

        if (width > height) {
            p.setTextSize(height / fontSizeC * scale + 0.5f); // convert dps
            // to pixels
        } else {
            p.setTextSize(width / fontSizeC * scale + 0.5f); // convert dps
            // to pixels
        }
        p.setTextAlign(Align.RIGHT);
        drawTextWithBackground(canvas, p, formattedCurrentDate, color, Color.BLACK, width, height);

        Matrix matrix2 = new Matrix();
        matrix2.postRotate(360 - rotation);
        sourceBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix2, false);

        bitmap.recycle();

        FileOutputStream outStream;
        outStream = new FileOutputStream(file);
        sourceBitmap.compress(Bitmap.CompressFormat.JPEG, jpegQuality, outStream);
        sourceBitmap.recycle();
        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * Corrects the orientation of a Bitmap. Orientation, depending of the device
 * , is not correctly set in the EXIF data of the taken image when it is saved
 * into disk.//from w w w  .  j  a  va  2s  .  c  o  m
 * 
 * Explanation:
 *    Camera orientation is not working ok (as is when capturing an image) because 
 *  OEMs do not adhere to the standard. So, each company does this following their 
 *  own way.
 * 
 * @param imagePath   path to the file
 * @return
 */
public static Bitmap media_correctImageOrientation(String imagePath) {
    Bitmap res = null;

    try {
        File f = new File(imagePath);
        ExifInterface exif = new ExifInterface(f.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle = 270;
        }

        Matrix mat = new Matrix();
        mat.postRotate(angle);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;

        Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
        res = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);

    } catch (OutOfMemoryError e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_correctImageOrientation() [OutOfMemory!]: " + e.getMessage(), e);
    } catch (Exception e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_correctImageOrientation(): " + e.getMessage(), e);
    } catch (Throwable e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_correctImageOrientation(): " + e.getMessage(), e);
    }

    return res;
}

From source file:com.android.launcher3.Utilities.java

private static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
    float left = (bmp2.getWidth() - (bmp1.getWidth() * ((float) bmp2.getHeight() / (float) bmp1.getHeight())))
            / (float) 2.0;
    float bmp1newW = bmp1.getWidth() * ((float) bmp2.getHeight() / (float) bmp1.getHeight());
    Bitmap bmp1new = Utils.getResizedBitmap(bmp1, bmp2.getHeight(), (int) bmp1newW);
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1new, left, 0, null);
    canvas.drawBitmap(bmp2, new Matrix(), null);
    return bmOverlay;
}

From source file:de.vanita5.twittnuker.util.Utils.java

/**
 * Resizes specific a Bitmap with keeping ratio.
 *//*from  w  w w . j  a  v  a  2  s  .  c om*/
public static Bitmap resizeBitmap(Bitmap orig, final int desireWidth, final int desireHeight) {
    final int width = orig.getWidth();
    final int height = orig.getHeight();

    if (0 < width && 0 < height && desireWidth < width || desireHeight < height) {
        // Calculate scale
        float scale;
        if (width < height) {
            scale = (float) desireHeight / (float) height;
            if (desireWidth < width * scale) {
                scale = (float) desireWidth / (float) width;
            }
        } else {
            scale = (float) desireWidth / (float) width;
        }

        // Draw resized image
        final Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        final Bitmap bitmap = Bitmap.createBitmap(orig, 0, 0, width, height, matrix, true);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(bitmap, 0, 0, null);

        orig = bitmap;
    }

    return orig;
}

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

private Bitmap loadThumbnail(Context context, Uri photoUri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;//from ww w .  ja  va  2 s  .  c o m
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

    int rotatedWidth, rotatedHeight;
    int orientation = getOrientation(context, photoUri);

    if (orientation == 90 || orientation == 270) {
        rotatedWidth = dbo.outHeight;
        rotatedHeight = dbo.outWidth;
    } else {
        rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
    }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (rotatedWidth > 1024 || rotatedHeight > 1024) {
        float widthRatio = ((float) rotatedWidth) / ((float) 1024);
        float heightRatio = ((float) rotatedHeight) / ((float) 1024);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    /*
     * if the orientation is not 0 (or -1, which means we don't know), we
     * have to do a rotation.
     */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix,
                true);
    }

    return srcBitmap;
}