android 拍照后照片旋转问题

1、拍照

String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
						File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");
						if (!file.exists()) {
							file.mkdirs();
							currentCameraPhotoFile = new File(file,name);
							if (!currentCameraPhotoFile.exists()) {
								currentCameraPhotoFile.createNewFile();
							}
						}
						Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
						intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentCameraPhotoFile));
						startActivityForResult(intent, CAMERA_WITH_DATA);

 2、在onActivityResult里处理返回结果,

case CAMERA_WITH_DATA: {// 拍照
				System.out.println("拍照返回。。。。。。。。。。。。。。。fileName"+currentCameraPhotoFile.getName());
				if (null!= currentCameraPhotoFile) {
					Bitmap cameraImageBitmap = BitmapUtil.getBitmapFromBigFile(currentCameraPhotoFile.getPath(), measuredWidth, measuredHeight);
					rl_jobnewbackground.setBackground(new BitmapDrawable(getResources(), cameraImageBitmap));
				
				}
				
				
				break;

 3、处理大图OOM和部分手机拍照结果旋转问题,直接上代码吧 

	/**
	 * 获取大图bitmap
	 * @param path 
	 * @param width
	 * @param height
	 * @return
	 */
	public static Bitmap getBitmapFromBigFile(String path, int width, int height) {
		//校正旋转角度
		 Matrix matrix = new Matrix();
		 matrix.postRotate(getBitmapDegree(path));
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		// BitmapFactory.decodeResource(res, resId, options);
		BitmapFactory.decodeFile(path, options);
		
		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, width, height);
	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;
	    Bitmap decodeFile = BitmapFactory.decodeFile(path, options);
		
	    decodeFile = Bitmap.createBitmap(decodeFile, 0, 0, decodeFile.getWidth(), decodeFile.getHeight(), matrix, true);
	
		return decodeFile;
	}
	/**
	 * 读取图片的旋转的角度
	 *
	 * @param path
	 *            图片绝对路径
	 * @return 图片的旋转角度
	 */
	public  static int getBitmapDegree(String path) {
	    int degree = 0;
	    try {
	        // 从指定路径下读取图片,并获取其EXIF信息
	        ExifInterface exifInterface = new ExifInterface(path);
	        // 获取图片的旋转信息
	        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
	                ExifInterface.ORIENTATION_NORMAL);
	        switch (orientation) {
	        case ExifInterface.ORIENTATION_ROTATE_90:
	            degree = 90;
	            break;
	        case ExifInterface.ORIENTATION_ROTATE_180:
	            degree = 180;
	            break;
	        case ExifInterface.ORIENTATION_ROTATE_270:
	            degree = 270;
	            break;
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return degree;
	}
	/**
	 * 计算出合理的inSampleSize
	 * @param options
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		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;
			}
		}

		return inSampleSize;
	}

猜你喜欢

转载自lxfengva.iteye.com/blog/2239719
今日推荐