首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传到服务器的[安卓]-Resize镜像

上传到服务器的[安卓]-Resize镜像
EN

Stack Overflow用户
提问于 2016-09-07 13:03:55
回答 3查看 14.3K关注 0票数 6

我的服务器限制上传图片大小(2MB)。我想上传图片从安卓设备到服务器。我想调整图像的大小。调整图像大小的最好方法是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-07 15:11:34

使用下面的代码。用"MAX_IMAGE_SIZE“指定你的最大文件大小,单位是千字节。在这段代码中,我首先调整图像的大小,然后压缩它。请参阅代码中的注释,以便更好地理解逻辑。

代码语言:javascript
复制
     public static String resizeAndCompressImageBeforeSend(Context context,String filePath,String fileName){
     final int MAX_IMAGE_SIZE = 700 * 1024; // max final file size in kilobytes

     // First decode with inJustDecodeBounds=true to check dimensions of image
     final BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(filePath,options);

     // Calculate inSampleSize(First we are going to resize the image to 800x800 image, in order to not have a big but very low quality image.
     //resizing the image will already reduce the file size, but after resizing we will check the file size and start to compress image
     options.inSampleSize = calculateInSampleSize(options, 800, 800);

     // Decode bitmap with inSampleSize set
     options.inJustDecodeBounds = false;
     options.inPreferredConfig= Bitmap.Config.ARGB_8888;

     Bitmap bmpPic = BitmapFactory.decodeFile(filePath,options);


     int compressQuality = 100; // quality decreasing by 5 every loop.
     int streamLength;
     do{
         ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
         Log.d("compressBitmap", "Quality: " + compressQuality);
         bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
         byte[] bmpPicByteArray = bmpStream.toByteArray();
         streamLength = bmpPicByteArray.length;
         compressQuality -= 5;
         Log.d("compressBitmap", "Size: " + streamLength/1024+" kb");
     }while (streamLength >= MAX_IMAGE_SIZE);

     try {
         //save the resized and compressed file to disk cache
         Log.d("compressBitmap","cacheDir: "+context.getCacheDir());
         FileOutputStream bmpFile = new FileOutputStream(context.getCacheDir()+fileName);
         bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile);
         bmpFile.flush();
         bmpFile.close();
     } catch (Exception e) {
         Log.e("compressBitmap", "Error on saving file");
     }
     //return the path of resized and compressed file
     return  context.getCacheDir()+fileName;
 }



 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
     String debugTag = "MemoryInformation";
     // Image nin islenmeden onceki genislik ve yuksekligi
     final int height = options.outHeight;
     final int width = options.outWidth;
     Log.d(debugTag,"image height: "+height+ "---image width: "+ width);
     int inSampleSize = 1;

     if (height > reqHeight || width > reqWidth) {

         final int halfHeight = height / 2;
         final int halfWidth = width / 2;

         // Calculate the largest inSampleSize value that is a power of 2 and keeps both
         // height and width larger than the requested height and width.
         while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
             inSampleSize *= 2;
         }
     }
     Log.d(debugTag,"inSampleSize: "+inSampleSize);
     return inSampleSize;
 }
票数 14
EN

Stack Overflow用户

发布于 2016-09-07 14:36:41

代码语言:javascript
复制
Bitmap scaledBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true);

缩容方法:

代码语言:javascript
复制
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
    boolean filter) {
float ratio = Math.min(
        (float) maxImageSize / realImage.getWidth(),
        (float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());

Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
        height, filter);
return newBitmap;

}

票数 0
EN

Stack Overflow用户

发布于 2018-09-04 17:18:56

您也可以在将图像编码为字符串后执行此操作,然后压缩其中的字节,然后在保存图像之前计算所需的长度。在这种情况下,如果它很大,用户将被迫更改图像。我假设你已经对你的图像进行了编码。

代码语言:javascript
复制
     long myBitmapLength = myEncodedImage.getBytes().length/1024;
       if(myBitmapLength < 200){
           //allow saving 
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39361550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档