我必须将从相机/画廊捕获的图像上传到服务器.在许多应用程序中,我看到分辨率为1000X560的图像大小为35 KB.以我为例,图像大小最大为380 KB.我的手机的相机拍摄的分辨率为2368X4224的图像尺寸小于< 2 MB.如何在保持较小尺寸的情况下获得高分辨率图像?到目前为止,这是我尝试过的:
BitmapFactory.Options bmOptions = new BitmapFactory.Options();bmOptions.inJustDecodeBounds = true;BitmapFactory.decodefile(realPath, bmOptions);bmOptions.inSampleSize = 1;bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;bmOptions.inJustDecodeBounds = false;Bitmap bitmap = BitmapFactory.decodefile(realPath, bmOptions);ByteArrayOutputStream bytes = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
我已经读过documentation.我面临的问题是如何确定图像的最小宽度和最小高度.
解决方法:
嘿你能检查http://voidcanvas.com/whatsapp-like-image-compression-in-android/ 这个链接
我认为这是最好的图像压缩教程.
还要检查此答案:https://stackoverflow.com/a/26928768/5275436
希望对您有所帮助.
编辑:这是图像调整大小.
// max Height and wIDth values of the compressed image is taken as 816x612 float maxHeight = 816.0f; float maxWIDth = 612.0f; float imgRatio = actualWIDth / actualHeight; float maxRatio = maxWIDth / maxHeight; // wIDth and height values are set maintaining the aspect ratio of the image if (actualHeight > maxHeight || actualWIDth > maxWIDth) { if (imgRatio < maxRatio) { imgRatio = maxHeight / actualHeight; actualWIDth = (int) (imgRatio * actualWIDth); actualHeight = (int) maxHeight; } else if (imgRatio > maxRatio) { imgRatio = maxWIDth / actualWIDth; actualHeight = (int) (imgRatio * actualHeight); actualWIDth = (int) maxWIDth; } else { actualHeight = (int) maxHeight; actualWIDth = (int) maxWIDth; } }
总结 以上是内存溢出为你收集整理的Android在上传到服务器之前减小图像大小全部内容,希望文章能够帮你解决Android在上传到服务器之前减小图像大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)