Java Code Examples for android.graphics.BitmapRegionDecoder#getWidth()

The following examples show how to use android.graphics.BitmapRegionDecoder#getWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorExtractionService.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private Palette getHotseatPalette() {
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    if (AndroidVersion.isAtLeastNougat) {
        try (ParcelFileDescriptor fd = wallpaperManager
                .getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
            BitmapRegionDecoder decoder = BitmapRegionDecoder
                    .newInstance(fd.getFileDescriptor(), false);
            int height = decoder.getHeight();
            Rect decodeRegion = new Rect(0, (int) (height * (1f - HOTSEAT_FRACTION)),
                    decoder.getWidth(), height);
            Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
            decoder.recycle();
            if (bitmap != null) {
                return Palette.from(bitmap).clearFilters().generate();
            }
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
    }

    Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
    return Palette.from(wallpaper)
            .setRegion(0, (int) (wallpaper.getHeight() * (1f - HOTSEAT_FRACTION)),
                    wallpaper.getWidth(), wallpaper.getHeight())
            .clearFilters()
            .generate();
}
 
Example 2
Source File: ColorExtractionService.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private Palette getStatusBarPalette() {
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    int statusBarHeight = getResources()
            .getDimensionPixelSize(R.dimen.status_bar_height);

    if (AndroidVersion.isAtLeastNougat) {
        try (ParcelFileDescriptor fd = wallpaperManager
                .getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
            BitmapRegionDecoder decoder = BitmapRegionDecoder
                    .newInstance(fd.getFileDescriptor(), false);
            Rect decodeRegion = new Rect(0, 0,
                    decoder.getWidth(), statusBarHeight);
            Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
            decoder.recycle();
            if (bitmap != null) {
                return Palette.from(bitmap).clearFilters().generate();
            }
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
    }

    Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
    return Palette.from(wallpaper)
            .setRegion(0, 0, wallpaper.getWidth(), statusBarHeight)
            .clearFilters()
            .generate();
}
 
Example 3
Source File: PictureResizeDialog.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
@Override
protected Bitmap doInBackground (final Void... params) {
	LOG.i("Generating preview: s=%s q=%s.", this.dlg.getScale(), this.dlg.getQuality());
	try {
		final Bitmap scaled = this.srcMetadata.getBitmap(this.dlg.getScale().getPercentage());
		final ByteArrayOutputStream compOut = new ByteArrayOutputStream(512 * 1024);
		if (scaled.compress(Bitmap.CompressFormat.JPEG, this.dlg.getQuality().getPercentage(), compOut)) {
			this.summary
					.append(this.srcMetadata.getWidth()).append(" x ").append(this.srcMetadata.getHeight())
					.append(" (").append(IoHelper.readableFileSize(this.srcMetadata.getSize())).append(")")
					.append(" --> ").append(scaled.getWidth()).append(" x ").append(scaled.getHeight())
					.append(" (").append(IoHelper.readableFileSize(compOut.size())).append(")");
			final BitmapRegionDecoder dec = BitmapRegionDecoder.newInstance(compOut.toBufferedInputStream(), true);
			try {
				final int srcW = dec.getWidth();
				final int srcH = dec.getHeight();
				final int tgtW = this.dlg.getRootView().getWidth(); // FIXME Workaround for ImageView width issue.  Fix properly with something like FixedWidthImageView.
				final int tgtH = this.imgPreview.getHeight();
				final int left = srcW > tgtW ? (srcW - tgtW) / 2 : 0;
				final int top = srcH > tgtH ? (srcH - tgtH) / 2 : 0;
				final BitmapFactory.Options options = new BitmapFactory.Options();
				options.inPurgeable = true;
				options.inInputShareable = true;
				return dec.decodeRegion(new Rect(left, top, left + tgtW, top + tgtH), options);
			}
			finally {
				dec.recycle();
			}
		}
		this.summary.append("Failed to compress image."); //ES
		return null;
	}
	// XXX Many cases of OutOfMemoryError have been reported, particularly on low end hardware.
	// Try not to upset the user too much by not dying completely if possible.
	catch (final Throwable e) {
		LOG.e("Failed to generate preview image.", e);
		this.summary.append(e.toString());
		return null;
	}
}