Code example for BitmapFactory.Options

Methods: decodeByteArraydecodeFile

0
 
		if (SDCardUtil.isSDCardExist()) { 
 
			LogUtils.logi(TAG, "1--" + System.currentTimeMillis());
 
			BitmapFactory.Options options = new BitmapFactory.Options();
			// 06-25 21:03:35.981: E/dalvikvm-heap(12440): Out of memory on a 
			// 8392808-byte allocation. 
			// options.inSampleSize = STANDARD_SCALE_RATIO; //先进行1/256压缩, 
			// 否则容易OOM。 
			// added by edwar 2012-06-25 
 
			// 1, 先生成 bitmap 
			// FileInputStream fileInputStream = null; 
			// try { 
			// fileInputStream = new FileInputStream(fName); 
			// } catch (FileNotFoundException e) { 
			// e.printStackTrace(); 
			// } 
			// LogUtils.logi(TAG, "1.1--"+System.currentTimeMillis()); 
			// 
			// //比调用系统路径拍照多了这一步,时间多耗时0.4秒左右 @see 
			// getScaledBitmapFromSystemCameraBundle() 
			// Bitmap mBitmap = 
			// BitmapFactory.decodeStream(fileInputStream,null,options); 
 
			options.inJustDecodeBounds = true;// 不加载bitmap到内存中
			BitmapFactory.decodeFile(fName, options);
			int outWidth = options.outWidth;
			int outHeight = options.outHeight;
			Log.i(TAG, "width - " + outWidth + ", height - " + outHeight);
			options.inDither = false;
			options.inPreferredConfig = Bitmap.Config.ARGB_8888;
			options.inSampleSize = 1;
 
			if (outWidth != 0 && outHeight != 0 && STANDARD_WIDTH != 0
					&& STANDARD_HEIGHT != 0) {
				// 这就是用来做缩放比的。这里有个技巧: 
				// inSampleSize=(outHeight/Height+outWidth/Width)/2 
				int sampleSize = (int) ((outWidth / STANDARD_WIDTH + outHeight
						/ STANDARD_HEIGHT) / 2);
				Log.d(TAG, "sampleSize = " + sampleSize);
				options.inSampleSize = sampleSize;
			} 
			options.inJustDecodeBounds = false;
			// 1,这种方法比上面被注释掉的方法省时 
			Bitmap mBitmap = BitmapFactory.decodeFile(fName, options);
 
			// 2, 在压缩图片质量 
			LogUtils.logi(TAG, "1.2--" + System.currentTimeMillis());
 
			if (mBitmap == null) {
				LogUtils.logi(TAG, "bm is null");
			} 
			int w = mBitmap.getWidth();
			int h = mBitmap.getHeight();
			int height = h;
			int width = w;
			LogUtils.logi(TAG, "width is - " + w + ", height is - " + h + "\n "
					+ "before scaled, size is - : " + (h * w));
 
			// 2,这里做了压缩处理,Bitmap2Bytes()调用Bitmap.compress()方法,唯一的压缩方法 
			// 注意,存储的时候,也做了压缩处理,@see PictureUtil.storePicture(mBitmap, fName); 
			// 所以,之前mkt360一共用了两次压缩。 
			byte[] mdata = PictureUtil.Bitmap2Bytes(mBitmap);
			LogUtils.logi(TAG, "1.3--" + System.currentTimeMillis());
 
			// 3, 这里再通过decodeByteArray将之前的bytes转换成bitmap 
			// 获取宽度缩放比例 
			// BitmapFactory.Options mOptions = new Options(); 
			// int scale_ratio = (int) (width / (float) STANDARD_WIDTH); 
			// LogUtils.logi(TAG, "ratio is - " + scale_ratio); 
			// if (scale_ratio <= 0) 
			// scale_ratio = 1; 
			// mOptions.inSampleSize = scale_ratio; 
			options.inJustDecodeBounds = false;
			options.inSampleSize = 1;
 
			mBitmap = BitmapFactory.decodeByteArray(mdata, 0, mdata.length,
					options);
			LogUtils.logi(TAG, "1.4--" + System.currentTimeMillis());
			if (mBitmap == null) {
				LogUtils.logi(TAG, "bm is null");
			}