Example usage for android.graphics Matrix postScale

List of usage examples for android.graphics Matrix postScale

Introduction

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

Prototype

public boolean postScale(float sx, float sy) 

Source Link

Document

Postconcats the matrix with the specified scale.

Usage

From source file:Main.java

public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
    boolean saveComplete = true;
    try {//from   w ww .ja v  a2 s  .c om
        f.createNewFile();
        FileOutputStream fOut = null;
        fOut = new FileOutputStream(f);
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        int finalWidth = 800;
        int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
        double x = width * finalHeight;
        double y = height * finalWidth;

        if (x > y) {
            finalHeight = (int) (y / (double) width);
        } else if (x < y) {
            finalWidth = (int) (x / (double) height);
        }

        if (finalWidth > width && finalHeight > height) {
            finalWidth = width;
            finalHeight = height;
        }
        Matrix matrix = new Matrix();
        matrix.reset();
        float scaleWidth = ((float) finalWidth) / (float) width;
        float scaleHeight = ((float) finalHeight) / (float) height;
        matrix.postScale(scaleWidth, scaleHeight);
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
        fOut.flush();
        fOut.close();
        mBitmap.recycle();
        System.gc();
    } catch (FileNotFoundException e) {
        saveComplete = false;
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        saveComplete = false;
    }
    return saveComplete;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

public static DbObject from(Context context, Uri videoUri) throws IOException {
    // Query gallery for camera picture via
    // Android ContentResolver interface
    ContentResolver cr = context.getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;/*from w  w  w  .  ja v  a 2 s .c om*/
    long videoId = Long.parseLong(videoUri.getLastPathSegment());
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(cr, videoId,
            MediaStore.Video.Thumbnails.MINI_KIND, options);
    int targetSize = 200;
    int width = curThumb.getWidth();
    int height = curThumb.getHeight();
    int cropSize = Math.min(width, height);
    float scaleSize = ((float) targetSize) / cropSize;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleSize, scaleSize);
    curThumb = Bitmap.createBitmap(curThumb, 0, 0, width, height, matrix, true);
    JSONObject base = new JSONObject();
    String localIp = ContentCorral.getLocalIpAddress();
    if (localIp != null) {
        try {
            // TODO: Security breach hack?
            base.put(Contact.ATTR_LAN_IP, localIp);
            base.put(LOCAL_URI, videoUri.toString());
            base.put(MIME_TYPE, cr.getType(videoUri));
        } catch (JSONException e) {
            Log.e(TAG, "impossible json error possible!");
        }
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    curThumb.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    byte[] data = baos.toByteArray();
    return from(base, data);
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
    if (bitmap == null)
        return bitmap;
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) width / bitmapWidth);
    float scaleHeight;
    if (height == 0)
        scaleHeight = scaleWidth;/*from w  w  w . j  a v  a  2  s  .  c o  m*/
    else
        scaleHeight = ((float) height / bitmapHeight);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
    return newbmp;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java

public static DbObject from(Context context, Uri imageUri) throws IOException {
    // Query gallery for camera picture via
    // Android ContentResolver interface
    ContentResolver cr = context.getContentResolver();

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   ww  w.j av  a  2s .c  o m
    BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options);

    int targetSize = 200;
    int xScale = (options.outWidth + targetSize - 1) / targetSize;
    int yScale = (options.outHeight + targetSize - 1) / targetSize;

    int scale = xScale < yScale ? xScale : yScale;
    //uncomment this to get faster power of two scaling
    //for(int i = 0; i < 32; ++i) {
    //   int mushed = scale & ~(1 << i);
    //   if(mushed != 0)
    //      scale = mushed;
    //}

    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;

    Bitmap sourceBitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options);

    int width = sourceBitmap.getWidth();
    int height = sourceBitmap.getHeight();
    int cropSize = Math.min(width, height);

    float scaleSize = ((float) targetSize) / cropSize;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleSize, scaleSize);
    float rotation = PhotoTaker.rotationForImage(context, imageUri);
    if (rotation != 0f) {
        matrix.preRotate(rotation);
    }

    Bitmap resizedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    byte[] data = baos.toByteArray();
    sourceBitmap.recycle();
    sourceBitmap = null;
    resizedBitmap.recycle();
    resizedBitmap = null;
    System.gc(); // TODO: gross.

    JSONObject base = new JSONObject();
    if (ContentCorral.CONTENT_CORRAL_ENABLED) {
        try {
            String type = cr.getType(imageUri);
            if (type == null) {
                type = "image/jpeg";
            }
            base.put(CorralClient.OBJ_LOCAL_URI, imageUri.toString());
            base.put(CorralClient.OBJ_MIME_TYPE, type);
            String localIp = ContentCorral.getLocalIpAddress();
            if (localIp != null) {
                base.put(Contact.ATTR_LAN_IP, localIp);
            }
        } catch (JSONException e) {
            Log.e(TAG, "impossible json error possible!");
        }
    }
    return from(base, data);
}

From source file:Main.java

public static Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {/*from www.  ja  v a 2 s  .c o m*/
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        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;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }

    return null;
}

From source file:Main.java

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
    if (null == drawable || w < 0 || h < 0) {
        return null;
    }//w w w. j a va  2  s .  c o  m

    try {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = drawableToBitmap(drawable);
        Matrix matrix = new Matrix();
        float scaleWidth = 1;
        float scaleHeight = 1;
        if (w > 0) {
            scaleWidth = ((float) w / width);
        }
        if (h > 0) {
            scaleHeight = ((float) h / height);
        }

        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
        return new BitmapDrawable(newbmp);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
    int newWidth = width;
    int newHeight = height;
    // if(newWidth < bitmap.getWidth()){
    // return bitmap;
    // }//from  ww  w . j  av  a2  s .com
    if (newHeight == 0) {
        newHeight = (int) (newWidth / (float) bitmap.getWidth() * bitmap.getHeight());
    }
    Bitmap result = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Matrix matrix = new Matrix();
    float scaleX = 1;
    float scaleY = 1;
    // if(newWidth>bitmap.getWidth()){
    scaleX = newWidth / (float) bitmap.getWidth();
    if (height != 0) {
        scaleY = newHeight / (float) bitmap.getHeight();
    } else {
        scaleY = scaleX;
    }
    // }
    matrix.postScale(scaleX, scaleY);
    canvas.drawBitmap(bitmap, matrix, null);
    return result;
}

From source file:com.kabootar.GlassMemeGenerator.ImageOverlay.java

public static boolean saveToSD(final Bitmap overlaid, final String sdPath, final String fileName) {
    boolean isItSaved = false;
    new AsyncTask<Void, Void, Void>() {

        @Override/*from w  w w  . j  a va2 s  . c om*/
        protected Void doInBackground(Void... arg0) {

            File image = new File(sdPath, fileName);

            FileOutputStream outStream;
            try {

                outStream = new FileOutputStream(image);
                //resize image
                Bitmap newoverlaid = getResizedBitmap(overlaid, 1000, 1362);
                newoverlaid.compress(Bitmap.CompressFormat.PNG, 100, outStream);

                outStream.flush();
                outStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // CREATE A MATRIX FOR THE MANIPULATION
            Matrix matrix = new Matrix();
            // RESIZE THE BIT MAP
            matrix.postScale(scaleWidth, scaleHeight);

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

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }
    }.execute();

    return isItSaved;
}

From source file:Main.java

public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
    try {/* w w  w.j a  va  2  s .co  m*/
        int orientation = getExifOrientation(src);

        if (orientation == 1) {
            return bitmap;
        }

        Matrix matrix = new Matrix();
        switch (orientation) {
        case 2:
            matrix.setScale(-1, 1);
            break;
        case 3:
            matrix.setRotate(180);
            break;
        case 4:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case 5:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case 6:
            matrix.setRotate(90);
            break;
        case 7:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case 8:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }

        try {
            Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
            bitmap.recycle();
            return oriented;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:com.nloko.android.Utils.java

public static Bitmap resize(Bitmap bitmap, int maxHeight, int maxWidth) {
    if (bitmap == null) {
        throw new IllegalArgumentException("bitmap");
    }//from w  ww. j  a v  a  2s.com

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

    if ((maxHeight > 0 && height <= maxHeight) && (maxWidth > 0 && width <= maxWidth)) {
        return bitmap;
    }

    int newHeight = height;
    int newWidth = width;

    float ratio;

    if (newHeight > maxHeight && maxHeight > 0) {
        ratio = (float) newWidth / (float) newHeight;
        newHeight = maxHeight;
        newWidth = Math.round(ratio * (float) newHeight);
    }

    if (newWidth > maxWidth && maxWidth > 0) {
        ratio = (float) newHeight / (float) newWidth;
        newWidth = maxWidth;
        newHeight = Math.round(ratio * (float) newWidth);
    }

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}