Code example for Bitmap

Methods: getConfiggetDensitygetHeightgetWidthrecycle

0
        Canvas canvas = new Canvas();
 
        Bitmap bitmap;
        Paint paint;
        if ((m == null) || m.isIdentity()) {
            bitmap = Bitmap.createBitmap(width, height, source.getConfig());
            paint = null;
        } else { 
            RectF rect = new RectF(0, 0, width, height);
            m.mapRect(rect);
            bitmap = Bitmap.createBitmap(
                    Math.round(rect.width()), Math.round(rect.height()), source.getConfig());
 
            canvas.translate(-rect.left, -rect.top);
            canvas.concat(m);
 
            paint = new Paint(Paint.FILTER_BITMAP_FLAG);
            if (!m.rectStaysRect()) {
                paint.setAntiAlias(true);
            } 
        } 
        bitmap.setDensity(source.getDensity());
        canvas.setBitmap(bitmap);
 
        Rect srcBounds = new Rect(x, y, x + width, y + height);
        RectF dstBounds = new RectF(0, 0, width, height);
        canvas.drawBitmap(source, srcBounds, dstBounds, paint);
        return bitmap;
    } 
 
    private void closeStream(Closeable stream) {
        if (stream != null) {
            try { 
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } 
        } 
    } 
 
    private Rect getBitmapBounds(Uri uri) {
        Rect bounds = new Rect();
        InputStream is = null;
 
        try { 
            is = context.getContentResolver().openInputStream(uri);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, options);
 
            bounds.right = options.outWidth;
            bounds.bottom = options.outHeight;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally { 
            closeStream(is);
        } 
 
        return bounds;
    } 
 
    private int getOrientation(Uri uri) {
        int orientation = 0;
        Cursor cursor = null;
        try { 
            cursor = context.getContentResolver().query(uri, IMAGE_PROJECTION, null, null, null);
            if ((cursor != null) && cursor.moveToNext()) {
                orientation = cursor.getInt(INDEX_ORIENTATION);
            } 
        } catch (Exception e) {
            // Ignore error for no orientation column; just use the default orientation value 0. 
        } finally { 
            if (cursor != null) {
                cursor.close();
            } 
        } 
        return orientation;
    } 
 
    /** 
     * Decodes bitmap (maybe immutable) that keeps aspect-ratio and spans most within the bounds. 
     */ 
    private Bitmap decodeBitmap(Uri uri, int width, int height) {
        InputStream is = null;
        Bitmap bitmap = null;
 
        try { 
            // TODO: Take max pixels allowed into account for calculation to avoid possible OOM. 
            Rect bounds = getBitmapBounds(uri);
            int sampleSize = Math.max(bounds.width() / width, bounds.height() / height);
            sampleSize = Math.min(sampleSize,
                    Math.max(bounds.width() / height, bounds.height() / width));
 
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = Math.max(sampleSize, 1);
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
 
            is = context.getContentResolver().openInputStream(uri);
            bitmap = BitmapFactory.decodeStream(is, null, options);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "FileNotFoundException: " + uri);
        } finally { 
            closeStream(is);
        } 
 
        // Ensure bitmap in 8888 format, good for editing as well as GL compatible. 
        if ((bitmap != null) && (bitmap.getConfig() != Bitmap.Config.ARGB_8888)) {
            Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            bitmap.recycle();
            bitmap = copy;
        } 
 
        if (bitmap != null) {
            // Scale down the sampled bitmap if it's still larger than the desired dimension. 
            float scale = Math.min((float) width / bitmap.getWidth(),
                    (float) height / bitmap.getHeight());
            scale = Math.max(scale, Math.min((float) height / bitmap.getWidth(),
                    (float) width / bitmap.getHeight()));
            if (scale < 1) {
                Matrix m = new Matrix();
                m.setScale(scale, scale);
                Bitmap transformed = createBitmap(
                        bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m);
                bitmap.recycle();
                return transformed;
            } 
        } 
        return bitmap;
    } 
Connect your IDE to all the code out there  Get Codota for Java