Code example for Bitmap

Methods: compressgetConfiggetHeightgetWidth

0
	 *  
	 * @param Bitmap 
	 * */ 
	public byte[] saveBitmap(Bitmap beforeCompress) throws IOException {
		/*�õ�ͼƬ�Ĵ�С*/ 
		Config config = beforeCompress.getConfig();
		int factor = 0;
		if(config.compareTo(Config.ALPHA_8) == 0) {
			factor = 1;
		} else if(config.compareTo(Config.ARGB_4444) == 0) {
			factor = 2;
		} else if (config.compareTo(Config.ARGB_8888) == 0) {
			factor = 4;
		} else if (config.compareTo(Config.RGB_565) == 0) {
			factor = 2;
		} 
		int byteSize = beforeCompress.getHeight() * 
				beforeCompress.getWidth() * factor;
		/*����quality��ֵ*/ 
		int quality;
		if (byteSize <= REQUERED_SIZE) {
			quality = 100;
		} else { 
			quality = REQUERED_SIZE * 100 / byteSize;
		} 
		/*ѹ������ͼƬת��Ϊbyte����*/ 
		byte[] compressedBmp = null;
        try{ 
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if(beforeCompress.compress(CompressFormat.JPEG, 
            		quality, out)) {
            	compressedBmp = out.toByteArray();
            	out.flush();
                out.close();
            }