首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android studio中将图片转换为所需的像素图片并上传到云端存储

如何在android studio中将图片转换为所需的像素图片并上传到云端存储
EN

Stack Overflow用户
提问于 2020-04-27 00:55:46
回答 1查看 308关注 0票数 1

目前,我正在从库中选择一张图片作为设备库中的Uri,然后将其转换为位图,然后根据Uri的大小将其转换为BAOS,然后将其上传到云存储。我想要的是云存储上的每个图像都是200*200像素的图像。

我的代码

代码语言:javascript
复制
File f = new File(resultUri.getPath());
        long sizeUri = f.length()/1024;

        int quality;
        if (sizeUri <= 300)
            quality = 30;
        else if (sizeUri <= 1000)
            quality = 10;
        else if (sizeUri <= 2000)
            quality = 10;
        else if (sizeUri <= 3000)
            quality = 10;
        else
            quality= 5;



        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        byte[] uploadbaos = baos.toByteArray();

        UploadThumbnail(); 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-27 01:01:09

你可以试试这个

代码语言:javascript
复制
public static String resizeAndCompressImageBeforeSend(Context context, String filePath, String fileName) {
    final int MAX_IMAGE_SIZE = 350 * 512; // 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;
}

static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    String debugTag = "MemoryInformation";
    // The width and height of the Image n before being processed
    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;
}

您可以使用它来调整位图的大小

代码语言:javascript
复制
    private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
    try {
        if (maxHeight > 0 && maxWidth > 0) {
            int width = image.getWidth();
            int height = image.getHeight();
            float ratioBitmap = (float) width / (float) height;
            float ratioMax = (float) maxWidth / (float) maxHeight;

            int finalWidth = maxWidth;
            int finalHeight = maxHeight;
            if (ratioMax > ratioBitmap) {
                finalWidth = (int) ((float) maxHeight * ratioBitmap);
            } else {
                finalHeight = (int) ((float) maxWidth / ratioBitmap);
            }
            image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
            return image;
        } else {
            return image;
        }
    } catch (Exception e) {
        return image;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61444593

复制
相关文章

相似问题

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