Search in sources :

Example 21 with Config

use of android.graphics.Bitmap.Config in project weiui by kuaifan.

the class SizeConfigStrategy method decrementBitmapOfSize.

private void decrementBitmapOfSize(Integer size, Bitmap removed) {
    Bitmap.Config config = removed.getConfig();
    NavigableMap<Integer, Integer> sizes = getSizesForConfig(config);
    Integer current = sizes.get(size);
    if (current == null) {
        throw new NullPointerException("Tried to decrement empty size" + ", size: " + size + ", removed: " + logBitmap(removed) + ", this: " + this);
    }
    if (current == 1) {
        sizes.remove(size);
    } else {
        sizes.put(size, current - 1);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config)

Example 22 with Config

use of android.graphics.Bitmap.Config in project GomoTest by suReZj.

the class PhotoProcessing method rotate.

public static Bitmap rotate(Bitmap bitmap, int angle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Config config = bitmap.getConfig();
    nativeInitBitmap(width, height);
    sendBitmapToNative(bitmap);
    if (angle == 90) {
        nativeRotate90();
        bitmap.recycle();
        bitmap = Bitmap.createBitmap(height, width, config);
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
    } else if (angle == 180) {
        nativeRotate180();
        bitmap.recycle();
        bitmap = Bitmap.createBitmap(width, height, config);
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
    } else if (angle == 270) {
        nativeRotate180();
        nativeRotate90();
        bitmap.recycle();
        bitmap = Bitmap.createBitmap(height, width, config);
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
    }
    return bitmap;
}
Also used : Config(android.graphics.Bitmap.Config)

Example 23 with Config

use of android.graphics.Bitmap.Config in project LibreraReader by foobnix.

the class Bitmaps method reuse.

public boolean reuse(final String nodeId, final BitmapRef orig, final Rect bitmapBounds, final boolean invert) {
    lock.writeLock().lock();
    try {
        final Bitmap origBitmap = orig.getBitmap();
        final Config cfg = useDefaultBitmapType ? DEF_BITMAP_TYPE : origBitmap.getConfig();
        if (cfg != this.config) {
            return false;
        }
        if (BitmapManager.partSize != this.partSize) {
            return false;
        }
        final BitmapRef[] oldBitmaps = this.bitmaps;
        final int oldBitmapsLength = LengthUtils.length(oldBitmaps);
        this.bounds = bitmapBounds;
        this.columns = (int) Math.ceil(bitmapBounds.width() / (float) partSize);
        this.rows = (int) Math.ceil(bitmapBounds.height() / (float) partSize);
        this.bitmaps = new BitmapRef[columns * rows];
        final int newsize = this.columns * this.rows;
        int i = 0;
        for (; i < newsize; i++) {
            if (i < oldBitmapsLength) {
                this.bitmaps[i] = oldBitmaps[i];
                if (this.bitmaps[i] != null && this.bitmaps[i].isRecycled()) {
                    BitmapManager.release(this.bitmaps[i]);
                    this.bitmaps[i] = null;
                }
            }
            if (this.bitmaps[i] == null) {
                this.bitmaps[i] = BitmapManager.getBitmap(nodeId + ":reuse:" + i, partSize, partSize, config);
            }
            this.bitmaps[i].getBitmap().eraseColor(Color.CYAN);
        }
        for (; i < oldBitmapsLength; i++) {
            BitmapManager.release(oldBitmaps[i]);
        }
        final boolean hasAlpha = origBitmap.hasAlpha();
        RawBitmap slice = threadSlices.get();
        if (slice == null || slice.pixels.length < partSize * partSize || slice.hasAlpha != hasAlpha) {
            slice = new RawBitmap(partSize, partSize, hasAlpha);
            threadSlices.set(slice);
        }
        int top = 0;
        for (int row = 0; row < rows; row++, top += partSize) {
            int left = 0;
            for (int col = 0; col < columns; col++, left += partSize) {
                final int index = row * columns + col;
                final BitmapRef b = bitmaps[index];
                final Bitmap bmp = b.getBitmap();
                if (row == rows - 1 || col == columns - 1) {
                    final int right = Math.min(left + partSize, bounds.width());
                    final int bottom = Math.min(top + partSize, bounds.height());
                    bmp.eraseColor(invert ? PagePaint.NIGHT.fillPaint.getColor() : PagePaint.DAY.fillPaint.getColor());
                    slice.retrieve(origBitmap, left, top, right - left, bottom - top);
                } else {
                    slice.retrieve(origBitmap, left, top, partSize, partSize);
                }
                if (invert) {
                    slice.invert();
                }
                slice.toBitmap(bmp);
            }
        }
        return true;
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config) PagePaint(org.ebookdroid.core.PagePaint)

Example 24 with Config

use of android.graphics.Bitmap.Config in project weiui by kuaifan.

the class TransformationUtils method fitCenter.

/**
 * An expensive operation to resize the given Bitmap down so that it fits within the given
 * dimensions maintain the original proportions.
 *
 * @param pool   The BitmapPool obtain a bitmap from.
 * @param inBitmap  The Bitmap to shrink.
 * @param width  The width in pixels the final image will fit within.
 * @param height The height in pixels the final image will fit within.
 * @return A new Bitmap shrunk to fit within the given dimensions, or toFit if toFit's width or
 * height matches the given dimensions and toFit fits within the given dimensions
 */
public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height) {
    if (inBitmap.getWidth() == width && inBitmap.getHeight() == height) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "requested target size matches input, returning input");
        }
        return inBitmap;
    }
    final float widthPercentage = width / (float) inBitmap.getWidth();
    final float heightPercentage = height / (float) inBitmap.getHeight();
    final float minPercentage = Math.min(widthPercentage, heightPercentage);
    // Round here in case we've decoded exactly the image we want, but take the floor below to
    // avoid a line of garbage or blank pixels in images.
    int targetWidth = Math.round(minPercentage * inBitmap.getWidth());
    int targetHeight = Math.round(minPercentage * inBitmap.getHeight());
    if (inBitmap.getWidth() == targetWidth && inBitmap.getHeight() == targetHeight) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "adjusted target size matches input, returning input");
        }
        return inBitmap;
    }
    // Take the floor of the target width/height, not round. If the matrix
    // passed into drawBitmap rounds differently, we want to slightly
    // overdraw, not underdraw, to avoid artifacts from bitmap reuse.
    targetWidth = (int) (minPercentage * inBitmap.getWidth());
    targetHeight = (int) (minPercentage * inBitmap.getHeight());
    Bitmap.Config config = getNonNullConfig(inBitmap);
    Bitmap toReuse = pool.get(targetWidth, targetHeight, config);
    // We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
    TransformationUtils.setAlpha(inBitmap, toReuse);
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "request: " + width + "x" + height);
        Log.v(TAG, "toFit:   " + inBitmap.getWidth() + "x" + inBitmap.getHeight());
        Log.v(TAG, "toReuse: " + toReuse.getWidth() + "x" + toReuse.getHeight());
        Log.v(TAG, "minPct:   " + minPercentage);
    }
    Matrix matrix = new Matrix();
    matrix.setScale(minPercentage, minPercentage);
    applyMatrix(inBitmap, toReuse, matrix);
    return toReuse;
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Paint(android.graphics.Paint) Config(android.graphics.Bitmap.Config)

Example 25 with Config

use of android.graphics.Bitmap.Config in project weiui by kuaifan.

the class TransformationUtils method rotateImageExif.

/**
 * Rotate and/or flip the image to match the given exif orientation.
 *
 * @param pool            A pool that may or may not contain an image of the necessary
 *                        dimensions.
 * @param inBitmap        The bitmap to rotate/flip.
 * @param exifOrientation the exif orientation [1-8].
 * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary.
 */
public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int exifOrientation) {
    if (!isExifOrientationRequired(exifOrientation)) {
        return inBitmap;
    }
    final Matrix matrix = new Matrix();
    initializeMatrixForRotation(exifOrientation, matrix);
    // From Bitmap.createBitmap.
    final RectF newRect = new RectF(0, 0, inBitmap.getWidth(), inBitmap.getHeight());
    matrix.mapRect(newRect);
    final int newWidth = Math.round(newRect.width());
    final int newHeight = Math.round(newRect.height());
    Bitmap.Config config = getNonNullConfig(inBitmap);
    Bitmap result = pool.get(newWidth, newHeight, config);
    matrix.postTranslate(-newRect.left, -newRect.top);
    applyMatrix(inBitmap, result, matrix);
    return result;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Paint(android.graphics.Paint) Config(android.graphics.Bitmap.Config)

Aggregations

Config (android.graphics.Bitmap.Config)34 Bitmap (android.graphics.Bitmap)31 Paint (android.graphics.Paint)9 Canvas (android.graphics.Canvas)6 Matrix (android.graphics.Matrix)4 TargetApi (android.annotation.TargetApi)2 BitmapFactory (android.graphics.BitmapFactory)2 Rect (android.graphics.Rect)2 RectF (android.graphics.RectF)2 Nullable (android.support.annotation.Nullable)2 NullBitmapException (org.andengine.util.exception.NullBitmapException)2 SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 PaintFlagsDrawFilter (android.graphics.PaintFlagsDrawFilter)1 Point (android.graphics.Point)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1