Lau*_*yer 9 android bitmap android-photos
标题听起来有点像"noob问题",但我非常清楚如何为Android编程,我只想弄清楚它是实现我想要的最佳方式.
我的用例是:用户拍摄照片并将其发送到我们的服务器,该服务器具有文件大小限制(这可能意味着我们必须直接在设备上调整照片大小).
看起来很简单吧?我的问题如下:
1)更好地使用可能崩溃的意图,因为一些相机应用程序使用屁股进行编码或使用cawc相机库建立基本视图"拍照并确认"?(我做了两个,我更喜欢意图,但我想对此有意见).
2)你如何处理文件大小限制?我的意思是让照片的大小非常容易File.length()(即使返回的值不是很正确)但是如果超过限制,你怎么能说重新调整大小的画面有多大?(你需要转换位图来调整它的大小,然后它就会出现很多问题OOMException而你无法计算磁盘上位图的最终大小,你需要压缩并将其写入磁盘并在之后分析新创建的文件).
谢谢你的帮助:D
我以前也做过同样的事情。
1.我使用意图来调用其他相机应用程序,并在 onActivityResult 内,我获取 URI 并根据需要对其进行处理。
我们确实调整了图片的大小,但我也保留了原始比例,并根据 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;
}
}
Run Code Online (Sandbox Code Playgroud)
该解决方案并不花哨,但这就是我在项目中所做的,到目前为止,我们在发布后没有遇到任何问题。