gpt4 book ai didi

android - 将相机拍摄的照片上传到有限大小的服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:56 25 4
gpt4 key购买 nike

标题听起来可能有点像“菜鸟问题”,但我非常了解如何为 Android 编程,我只是想弄清楚实现我想要的东西的最佳方法是什么。

我的用例是:用户拍摄一张照片并将其发送到我们的服务器,但有文件大小限制(这可能意味着我们必须直接在设备上调整照片的大小)。

看起来很简单,对吧? 我的问题如下:

1) 更好地使用可能崩溃的 Intent ,因为一些相机应用程序是用 ass 编码的,或者使用 cawc 相机库构建基本 View “拍照并确认”? (我做了两个,我更喜欢 Intent ,但我想对此发表意见)。

2) 您如何处理文件大小限制?我的意思是使用 File.length() 很容易获得照片的大小(即使返回的值不完全正确)但是如果你超过了限制,你怎么能说 调整后的图片有多大?(您需要在位图中转换以调整它的大小,然后 OOMException 会出现很多问题,您无法计算位图的最终大小磁盘,需要先压缩写入磁盘,然后分析新建的文件)。

谢谢你的帮助:D

最佳答案

我之前也做过同样的事情。

1.我使用 intent 调用其他相机应用程序,并在 onActivityResult 内部,我取回 URI 并根据需要对其进行处理。

  1. 我们确实调整了图片的大小,但我也保留了原始比例,并根据 exif 数据对其进行了旋转。希望这个调整大小的代码块可以给你一些提示。

    public static Bitmap DecodeImage(String path, int resolution) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, opts);
    opts.inSampleSize = computeSampleSize(opts, -1, resolution);
    opts.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, opts);
    }

    public static int computeSampleSize(BitmapFactory.Options options,
    int minSideLength, int maxNumOfPixels) {
    int initialSize = computeInitialSampleSize(options, minSideLength,
    maxNumOfPixels);

    int roundedSize;

    if (initialSize <= 8) {
    roundedSize = 1;
    while (roundedSize < initialSize) {
    roundedSize <<= 1;
    }
    } else {
    roundedSize = (initialSize + 7) / 8 * 8;
    }

    return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels == -1) ? 1 :
    (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));

    int upperBound = (minSideLength == -1) ? 128 :
    (int) Math.min(Math.floor(w / minSideLength),
    Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
    // return the larger one when there is no overlapping zone.
    return lowerBound;
    }

    if ((maxNumOfPixels == -1) &&

    (minSideLength == -1)) {
    return 1;
    } else if (minSideLength == -1) {
    return lowerBound;
    } else {
    return upperBound;
    }

    }

    解决方案并不花哨,但我在项目中就是这样做的,发布后目前我们没有遇到任何问题。

关于android - 将相机拍摄的照片上传到有限大小的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30355257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com