Android Desire HD 位图缩放

发布于 2024-12-13 05:39:01 字数 1704 浏览 0 评论 0原文

我构建了一个自定义相机活动,用户可以在其中拍照。通过图片回调将图片保存到外部存储目录中。然后,用户被重定向到另一个活动,他可以在其中预览图片。在显示预览之前,图片会在单独的线程中按比例缩小到某个最大尺寸。这是缩放代码:

byte[] tempStorage = new byte[16 * 1024];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);

int oldWidth = options.outWidth;
int oldHeight = options.outHeight;

if (oldWidth > MAX_DIM || oldHeight > MAX_DIM) {

    Logger.i(Constants.SCALE_TAG, "Must scale picture");
    int scaleFactor = getScaleFactor(oldWidth, oldHeight);
    options.inJustDecodeBounds = false;
    options.inSampleSize = scaleFactor;
    options.inTempStorage = tempStorage;
    System.gc();
    newB = BitmapFactory.decodeFile(imagePath, options);

} else {

    Logger.i(Constants.SCALE_TAG, "No need for scaling");

    // We do not need to scale the picture, it is small
    // enough.
    System.gc();
    options.inJustDecodeBounds = false;
    options.inTempStorage = tempStorage;
    if (!stop) {
        newB = BitmapFactory.decodeFile(imagePath, options);
    } else {
        return;
    }

}

这是计算缩放系数的代码:

private int getScaleFactor(int oldWidth, int oldHeight) {

    // Holds the raw scale factor.
    float rawScale;

    // Get the scaling factor.
    if (oldWidth >= oldHeight) {
        rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldWidth * 1.0F));
    } else {
        rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldHeight * 1.0F)); 
    }
    Logger.i(Constants.SCALE_TAG, "Raw scale factor: " + rawScale);
    return Math.round(rawScale);

}

该代码在大多数设备上运行良好,但不知何故 Desire HD 无法正确缩放图片。看起来他把原始图片复制了三十次左右,然后将其压缩到位图中,形成了奇怪的条纹状图片。有人知道这个问题吗?

I've build a custom camera activity where the user can take a picture. The picture is saved in the external storage directory via the picture callback. The user is then redirected to another activity where he can preview the picture. Before the preview is shown, the picture is scaled down in a seperate thread to a certain maximum dimension. Here is the scaling code:

byte[] tempStorage = new byte[16 * 1024];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);

int oldWidth = options.outWidth;
int oldHeight = options.outHeight;

if (oldWidth > MAX_DIM || oldHeight > MAX_DIM) {

    Logger.i(Constants.SCALE_TAG, "Must scale picture");
    int scaleFactor = getScaleFactor(oldWidth, oldHeight);
    options.inJustDecodeBounds = false;
    options.inSampleSize = scaleFactor;
    options.inTempStorage = tempStorage;
    System.gc();
    newB = BitmapFactory.decodeFile(imagePath, options);

} else {

    Logger.i(Constants.SCALE_TAG, "No need for scaling");

    // We do not need to scale the picture, it is small
    // enough.
    System.gc();
    options.inJustDecodeBounds = false;
    options.inTempStorage = tempStorage;
    if (!stop) {
        newB = BitmapFactory.decodeFile(imagePath, options);
    } else {
        return;
    }

}

Here is the code that calculates the scaling factor:

private int getScaleFactor(int oldWidth, int oldHeight) {

    // Holds the raw scale factor.
    float rawScale;

    // Get the scaling factor.
    if (oldWidth >= oldHeight) {
        rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldWidth * 1.0F));
    } else {
        rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldHeight * 1.0F)); 
    }
    Logger.i(Constants.SCALE_TAG, "Raw scale factor: " + rawScale);
    return Math.round(rawScale);

}

This code works fine on most devices, but somehow the Desire HD won't correctly scale the picture. It looks like he takes the original picture and duplicates it thirty times or so and squizes it into the Bitmap, causing a weird stripe-like picture. Does anyone know of this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2024-12-20 05:39:01

你应该使用更少的内存。问题是你使用了太多内存。

You should use less memory. The problem is you're using too much memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文