Android, Compressing an image

后端 未结 3 1781
栀梦
栀梦 2021-01-14 14:54

I am sending an image over the network via wifi or the mobile network to be stored in a server and retrieved again. I\'ve done that but due to the size of images taken by th

3条回答
  •  再見小時候
    2021-01-14 15:25

    Try using the following method:

        //decodes image and scales it to reduce memory consumption
        //NOTE: if the image has dimensions which exceed int width and int height
        //its dimensions will be altered.
        private Bitmap decodeToLowResImage(byte [] b, int width, int height) {
            try {
                //Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o);
    
                //The new size we want to scale to
                final int REQUIRED_SIZE_WIDTH=(int)(width*0.7);
                final int REQUIRED_SIZE_HEIGHT=(int)(height*0.7);
    
                //Find the correct scale value. It should be the power of 2.
                int width_tmp=o.outWidth, height_tmp=o.outHeight;
                int scale=1;
                while(true){
                    if(width_tmp/2

提交回复
热议问题