Android 中的裁剪图片

发布于 2024-10-20 15:50:21 字数 540 浏览 3 评论 0原文

我已经尝试这个有一段时间了,我想从位图创建壁纸。假设所需的壁纸尺寸为 320x480,源图像尺寸为 2048x2048。

我不确定“裁剪适合”是否是正确的术语,但我想要实现的是获得与所需壁纸尺寸(320x480)具有相同比例的图片的大部分。

因此,在这种情况下,我想从源 Bitmap 中获取 2048x1365 或(准确地说是 1365.333...),并将其缩小到 320x480。

我尝试过的技术是:

1)首先将位图裁剪为2048x1365

bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);

2)将其缩小到320x480,

bm = Bitmap.createScaledBitmap(bm, 320, 480, false);

这会产生OutOfMemory错误。

有什么办法可以实现这一点吗?

问候,

德祖尔

I've been trying this for some time, I would like to create a wallpaper from a Bitmap. Let's say the desired wallpaper size is 320x480, and the source image size is 2048x2048.

I'm not sure whether crop-to-fit is the right term, but what I would like to achieve is to get most part of the picture that has the equal ratio as the desired wallpaper size (320x480).

So in this case, I would like to get 2048x1365 or (1365.333... to be exact) from the source Bitmap, and scale it down to 320x480.

The technique that I have tried is:

1) Crop the Bitmap into 2048x1365 first

bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);

2) Scale it down to 320x480

bm = Bitmap.createScaledBitmap(bm, 320, 480, false);

which produced OutOfMemory error.

Is there any way to achieve this?

Regards,

dezull

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

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

发布评论

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

评论(3

·深蓝 2024-10-27 15:50:21

感谢开源,我从Android Gallery源代码中找到了答案 这里在第230行:-D

croppedImage = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(croppedImage);

Rect srcRect = mCrop.getCropRect();
Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);

int dx = (srcRect.width() - dstRect.width()) / 2;
int dy = (srcRect.height() - dstRect.height()) / 2;

// If the srcRect is too big, use the center part of it.
srcRect.inset(Math.max(0, dx), Math.max(0, dy));

// If the dstRect is too big, use the center part of it.
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));

// Draw the cropped bitmap in the center
canvas.drawBitmap(mBitmap, srcRect, dstRect, null);

Thanks to open source, I found the answer from Android Gallery source code here at line 230 :-D

croppedImage = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(croppedImage);

Rect srcRect = mCrop.getCropRect();
Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);

int dx = (srcRect.width() - dstRect.width()) / 2;
int dy = (srcRect.height() - dstRect.height()) / 2;

// If the srcRect is too big, use the center part of it.
srcRect.inset(Math.max(0, dx), Math.max(0, dy));

// If the dstRect is too big, use the center part of it.
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));

// Draw the cropped bitmap in the center
canvas.drawBitmap(mBitmap, srcRect, dstRect, null);
墨小沫ゞ 2024-10-27 15:50:21

我知道这是一个非常晚的回复,但也许是这样的:

public static Bitmap scaleCropToFit(Bitmap original, int targetWidth, int targetHeight){
    //Need to scale the image, keeping the aspect ration first
    int width = original.getWidth();
    int height = original.getHeight();

    float widthScale = (float) targetWidth / (float) width;
    float heightScale = (float) targetHeight / (float) height;
    float scaledWidth;
    float scaledHeight;

    int startY = 0;
    int startX = 0;

    if (widthScale > heightScale) {
        scaledWidth = targetWidth;
        scaledHeight = height * widthScale;
        //crop height by...
        startY = (int) ((scaledHeight - targetHeight) / 2);
    } else {
        scaledHeight = targetHeight;
        scaledWidth = width * heightScale;
        //crop width by..
        startX = (int) ((scaledWidth - targetWidth) / 2);
    }

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(original, (int) scaledWidth, (int) scaledHeight, true);

    Bitmap resizedBitmap = Bitmap.createBitmap(scaledBitmap, startX, startY, targetWidth, targetHeight);
    return resizedBitmap;
}

I know this is an incredibly late reply, but something like this maybe:

public static Bitmap scaleCropToFit(Bitmap original, int targetWidth, int targetHeight){
    //Need to scale the image, keeping the aspect ration first
    int width = original.getWidth();
    int height = original.getHeight();

    float widthScale = (float) targetWidth / (float) width;
    float heightScale = (float) targetHeight / (float) height;
    float scaledWidth;
    float scaledHeight;

    int startY = 0;
    int startX = 0;

    if (widthScale > heightScale) {
        scaledWidth = targetWidth;
        scaledHeight = height * widthScale;
        //crop height by...
        startY = (int) ((scaledHeight - targetHeight) / 2);
    } else {
        scaledHeight = targetHeight;
        scaledWidth = width * heightScale;
        //crop width by..
        startX = (int) ((scaledWidth - targetWidth) / 2);
    }

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(original, (int) scaledWidth, (int) scaledHeight, true);

    Bitmap resizedBitmap = Bitmap.createBitmap(scaledBitmap, startX, startY, targetWidth, targetHeight);
    return resizedBitmap;
}
┊风居住的梦幻卍 2024-10-27 15:50:21

这是一个可以帮助您大部分实现目标的答案:
如何在 Android 中裁剪图像?

here is an answer that gets you most of the way there:
How to crop an image in android?

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