java - 在 Bitmapfactory 中解码图像的特定区域?

标签 java android bitmapfactory bitmapregiondecoder

我使用的 GeoTiff/PNG 文件太大,无法在我的代码中作为一个整体处理。

是否有可能在 bitmapfactory 中解码文件的特定区域(例如,由两个 x,y 坐标给出)?在 http://developer.android.com/reference/android/graphics/BitmapFactory.html(Android 的开发人员引用)中没有发现类似的内容。

谢谢!


根据 kcoppock 的提示,我设置了以下解决方案。

虽然我想知道为什么 rect 需要用 Rect(left, bottom, right, top) 而不是 Rect(left, top,右下角)...

调用示例:

Bitmap myBitmap = loadBitmapRegion(context, R.drawable.heightmap,
    0.08f, 0.32f, 0.13f, 0.27f);

功能:

public static Bitmap loadBitmapRegion(
    Context context, int resourceID,
    float regionLeft, float regionTop,
    float regionRight, float regionBottom) {

    // Get input stream for resource
    InputStream is = context.getResources().openRawResource(resourceID);

    // Set options
    BitmapFactory.Options opt = new BitmapFactory.Options();
    //opt.inPreferredConfig = Bitmap.Config.ARGB_8888; //standard

    // Create decoder
    BitmapRegionDecoder decoder = null;
    try {
        decoder = BitmapRegionDecoder.newInstance(is, false);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Get resource dimensions
    int h = decoder.getHeight();
    int w = decoder.getWidth();

    // Set region to decode
    Rect region = new Rect(
            Math.round(regionLeft*w), Math.round(regionBottom*h),
            Math.round(regionRight*w), Math.round(regionTop*h));

    // Return bitmap
    return decoder.decodeRegion(region, opt);

}

最佳答案

你应该看看BitmapRegionDecoder .它似乎准确描述了您正在寻找的用例。

关于java - 在 Bitmapfactory 中解码图像的特定区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192179/

相关文章:

java - Arraylist 值未出现在 HashMap 中

java - TextView 布局上方的图标下划线

android - 多个通知的未决意向

android - Android 应用中使用 RecognizerIntent 的数据安全性

java - 位图大小超出 Android 中的 VM 预算

java - 将渲染偏移量添加到 Frame

java - 如何在 Java Spring 应用程序成功启动时添加日志?

android - java.lang.NoSuchMethodError : No static method getDrawable(Landroid/content/Context;I)

android - 位图被垃圾收集后是否可以使用?

android - 更改位图的样本大小