Search in sources :

Example 6 with BitmapRegionDecoder

use of android.graphics.BitmapRegionDecoder in project android-crop by jdamcd.

the class CropImageActivity method decodeRegionCrop.

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();
    InputStream is = null;
    Bitmap croppedImage = null;
    try {
        is = getContentResolver().openInputStream(sourceUri);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();
        if (exifRotation != 0) {
            // Adjust crop area to account for image rotation
            Matrix matrix = new Matrix();
            matrix.setRotate(-exifRotation);
            RectF adjusted = new RectF();
            matrix.mapRect(adjusted, new RectF(rect));
            // Adjust to account for origin at 0,0
            adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
            rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right, (int) adjusted.bottom);
        }
        try {
            croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
            if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) {
                Matrix matrix = new Matrix();
                matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
            }
        } catch (IllegalArgumentException e) {
            // Rethrow with some extra information
            throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + "," + height + "," + exifRotation + ")", e);
        }
    } catch (IOException e) {
        Log.e("Error cropping image: " + e.getMessage(), e);
        setResultException(e);
    } catch (OutOfMemoryError e) {
        Log.e("OOM cropping image: " + e.getMessage(), e);
        setResultException(e);
    } finally {
        CropUtil.closeSilently(is);
    }
    return croppedImage;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Matrix(android.graphics.Matrix) InputStream(java.io.InputStream) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) IOException(java.io.IOException)

Example 7 with BitmapRegionDecoder

use of android.graphics.BitmapRegionDecoder in project ion by koush.

the class LoadDeepZoom method onCompleted.

@Override
public void onCompleted(Exception e, final Response<File> response) {
    if (e == null)
        e = response.getException();
    if (e != null) {
        report(e, null);
        return;
    }
    final File tempFile = response.getResult();
    if (ion.bitmapsPending.tag(key) != this) {
        //            Log.d("IonBitmapLoader", "Bitmap load cancelled (no longer needed)");
        return;
    }
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            FileInputStream fin = null;
            try {
                File file;
                // file cache will be null if the file is on the local file system already
                if (fileCache != null) {
                    fileCache.commitTempFiles(key, tempFile);
                    file = fileCache.getFile(key);
                } else {
                    // local file system, use the "temp" file as the source.
                    file = tempFile;
                }
                BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(file, 0, 0);
                final Point size = new Point(options.outWidth, options.outHeight);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    fin = fileCache.get(key);
                    GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(fin)));
                    GifFrame frame = gifDecoder.nextFrame();
                    BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
                    info.gifDecoder = gifDecoder;
                    report(null, info);
                    return;
                }
                BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(file.toString(), false);
                Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, size.x, size.y), options);
                if (bitmap == null)
                    throw new Exception("unable to load decoder");
                BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                info.decoder = decoder;
                info.decoderFile = file;
                info.servedFrom = response.getServedFrom();
                report(null, info);
            } catch (Exception e) {
                report(e, null);
            } finally {
                StreamUtility.closeQuietly(fin);
            }
        }
    });
}
Also used : Rect(android.graphics.Rect) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) Point(android.graphics.Point) FileInputStream(java.io.FileInputStream) GifDecoder(com.koushikdutta.ion.gif.GifDecoder) Bitmap(android.graphics.Bitmap) GifFrame(com.koushikdutta.ion.gif.GifFrame) File(java.io.File) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 8 with BitmapRegionDecoder

use of android.graphics.BitmapRegionDecoder in project GalleryFinal by pengjianbo.

the class CropImageActivity method decodeRegionCrop.

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();
    InputStream is = null;
    Bitmap croppedImage = null;
    try {
        is = getContentResolver().openInputStream(sourceUri);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();
        if (exifRotation != 0) {
            // Adjust crop area to account for image rotation
            Matrix matrix = new Matrix();
            matrix.setRotate(-exifRotation);
            RectF adjusted = new RectF();
            matrix.mapRect(adjusted, new RectF(rect));
            // Adjust to account for origin at 0,0
            adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
            rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right, (int) adjusted.bottom);
        }
        try {
            croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
            if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) {
                Matrix matrix = new Matrix();
                matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
            }
        } catch (IllegalArgumentException e) {
            // Rethrow with some extra information
            throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + "," + height + "," + exifRotation + ")", e);
        }
    } catch (IOException e) {
        ILogger.e(e);
        setCropSaveException(e);
    } catch (OutOfMemoryError e) {
        ILogger.e(e);
        setCropSaveException(e);
    } finally {
        CropUtil.closeSilently(is);
    }
    return croppedImage;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Matrix(android.graphics.Matrix) InputStream(java.io.InputStream) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) IOException(java.io.IOException)

Example 9 with BitmapRegionDecoder

use of android.graphics.BitmapRegionDecoder in project weiciyuan by qii.

the class ImageUtility method getMiddlePictureInTimeLine.

public static Bitmap getMiddlePictureInTimeLine(String url, int reqWidth, int reqHeight, FileDownloaderHttpHelper.DownloadListener downloadListener) throws WeiboException {
    try {
        String filePath = FileManager.getFilePathFromUrl(url, FileLocationMethod.picture_bmiddle);
        File file = new File(filePath);
        if (!file.exists() && !SettingUtility.isEnablePic()) {
            return null;
        }
        if (!file.exists()) {
            getBitmapFromNetWork(url, filePath, downloadListener);
        }
        if (isGif(filePath)) {
            return getMiddlePictureInTimeLineGif(filePath, reqWidth, reqHeight);
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        int height = options.outHeight;
        int width = options.outWidth;
        int cutHeight = 0;
        int cutWidth = 0;
        if (height >= reqHeight && width >= reqWidth) {
            cutHeight = reqHeight;
            cutWidth = reqWidth;
        } else if (height < reqHeight && width >= reqWidth) {
            cutHeight = height;
            cutWidth = (reqWidth * cutHeight) / reqHeight;
        } else if (height >= reqHeight && width < reqWidth) {
            cutWidth = width;
            cutHeight = (reqHeight * cutWidth) / reqWidth;
        } else if (height < reqHeight && width < reqWidth) {
            float betweenWidth = ((float) reqWidth - (float) width) / (float) width;
            float betweenHeight = ((float) reqHeight - (float) height) / (float) height;
            if (betweenWidth > betweenHeight) {
                cutWidth = width;
                cutHeight = (reqHeight * cutWidth) / reqWidth;
            } else {
                cutHeight = height;
                cutWidth = (reqWidth * cutHeight) / reqHeight;
            }
        }
        if (cutWidth > 0 && cutHeight > 0) {
            int startX = 0;
            if (cutWidth < width) {
                startX = (width - cutWidth) / 2;
            }
            try {
                BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(filePath, false);
                if (decoder != null) {
                    Bitmap bitmap = decoder.decodeRegion(new Rect(startX, 0, startX + cutWidth, cutHeight), null);
                    if (bitmap.getHeight() < reqHeight && bitmap.getWidth() < reqWidth) {
                        Bitmap scale = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
                        if (scale != bitmap) {
                            bitmap.recycle();
                            bitmap = scale;
                        }
                    }
                    if (bitmap != null) {
                        Bitmap roundedCornerBitmap = ImageEditUtility.getRoundedCornerBitmap(bitmap);
                        if (roundedCornerBitmap != bitmap) {
                            bitmap.recycle();
                            bitmap = roundedCornerBitmap;
                        }
                        return bitmap;
                    }
                }
            } catch (IOException ignored) {
            }
        }
        return null;
    } catch (OutOfMemoryError ignored) {
        ignored.printStackTrace();
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) File(java.io.File)

Example 10 with BitmapRegionDecoder

use of android.graphics.BitmapRegionDecoder in project AisenWeiBo by wangdan.

the class TimelineBitmapCompress method compress.

@Override
public Bitmap compress(byte[] bitmapBytes, File file, String url, ImageConfig config, int origW, int origH) throws Exception {
    Logger.v("ATimeline", "压缩小图片");
    Bitmap bitmap = null;
    int maxWidth = config.getMaxWidth() == 0 ? SystemUtils.getScreenWidth(GlobalContext.getInstance()) : config.getMaxWidth();
    int maxHeight = config.getMaxHeight() == 0 ? SystemUtils.getScreenHeight(GlobalContext.getInstance()) : config.getMaxHeight();
    // 如果高度比宽度在2倍以上,取高度的一部分
    if (origH * 1.0f / origW > 2) {
        int reqHeight = maxHeight;
        // 截取局部图片
        BitmapRegionDecoder bitmapDecoder = BitmapRegionDecoder.newInstance(bitmapBytes, 0, bitmapBytes.length, true);
        Rect rect = new Rect(0, 0, origW, reqHeight);
        bitmap = bitmapDecoder.decodeRegion(rect, null).copy(Config.ARGB_8888, true);
    } else {
        bitmap = BitmapDecoder.decodeSampledBitmapFromByte(bitmapBytes, maxWidth, maxHeight);
    }
    Logger.d(TAG, String.format("bitmap width = %d, height = %d", bitmap.getWidth(), bitmap.getHeight()));
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder)

Aggregations

BitmapRegionDecoder (android.graphics.BitmapRegionDecoder)22 Rect (android.graphics.Rect)19 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)15 BitmapFactory (android.graphics.BitmapFactory)13 InputStream (java.io.InputStream)12 Matrix (android.graphics.Matrix)10 RectF (android.graphics.RectF)10 Paint (android.graphics.Paint)7 FileInputStream (java.io.FileInputStream)6 Resources (android.content.res.Resources)5 Canvas (android.graphics.Canvas)5 Point (android.graphics.Point)5 BitmapDrawable (android.graphics.drawable.BitmapDrawable)5 DeadSystemException (android.os.DeadSystemException)5 BufferedInputStream (java.io.BufferedInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 RemoteException (android.os.RemoteException)4 ErrnoException (android.system.ErrnoException)4