android bitmap compression

	/**
	 * convert bitmap to base64
	 *
	 * @param bitmap
	 * @return
	 */
	public static String bitmapToBase64(Bitmap bitmap) {
		String result = null;
		ByteArrayOutputStream baos = null;
		try {
			if (bitmap != null) {
				baos = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

				baos.flush();
				baos.close();

				byte[] bitmapBytes = baos.toByteArray();
				result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
			}
		} catch (IOException e) {
			e.printStackTrace ();
		} finally {
			try {
				if (baos != null) {
					baos.flush();
					baos.close();
				}
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}
		return result;
	}

	private Bitmap comp(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();        
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        if( baos.toByteArray().length / 1024>1024) {//Judging that if the image is larger than 1M, compress it to avoid overflow when generating the image (BitmapFactory.decodeStream)    
            baos.reset();//Reset baos to clear baos
            image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//Compress 50% here, and store the compressed data in baos
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //Start to read the picture, set options.inJustDecodeBounds back to true at this time
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        //Now mainstream mobile phones are mostly 800*480 resolution, so we set the height and width to
        float hh = 800f;//Here the height is set to 800f
        float ww = 480f;//The width is set to 480f here
        // Zoom ratio. Since it is a fixed scale scaling, only one data of height or width can be used for calculation.
        int be = 1;//be=1 means no scaling
        if (w > h && w > ww) {//If the width is large, it will be scaled according to the fixed size of the width
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//If the height is high, it is scaled according to the fixed size of the width
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;//Set the zoom ratio
        newOpts.inPreferredConfig = Config.RGB_565;//Reduce the picture from ARGB888 to RGB565
        //Re-read the picture, note that options.inJustDecodeBounds has been set back to false at this time
        isBm = new ByteArrayInputStream(baos.toByteArray());
        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        return compressImage(bitmap);//Compress the scale and then perform quality compression
    }
	
	private Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//Quality compression method, where 100 means no compression, store the compressed data in baos
        int options = 100;
        while ( baos.toByteArray().length / 1024>100) { //Loop to determine if the compressed image is larger than 100kb, and continue to compress        
            baos.reset();//Reset baos to clear baos
            options -= 10;//Reduce by 10 each time
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//compress options% here, and store the compressed data in baos

        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//Store the compressed data baos in ByteArrayInputStream
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//Generate ByteArrayInputStream data into pictures
        return bitmap;
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326254536&siteId=291194637