Android调用系统相机并得到图片两种方法

如题,同时由于项目联系需要所写出的方法。

方法一的思路是根据返回的intent对象获取图片信息,代码如下:

intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQ_CODE_CAMERA);

这两句代码即可通知Android系统打开相机,然后通过代码:

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			Bundle mBundle = data.getExtras();
			Bitmap mBitmap = (Bitmap) mBundle.get("data");
			iv.setImageBitmap(mBitmap);
		}
<span style="white-space:pre">	</span>}

这样就可以给指定的ImageView设置图片了。


方法二的思路是在打开相机时,明确相机保存照片的路径,从路径在获取图片对象,代码如下:

			imagePath = IMG_ROOT + File.separator + 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题的原因是由于不同的设备在拍照后保存图片的方向不同,导致在显示图片时出现了旋转的情况。为了解决这个问题,可以通过以下方法进行处理: 1. 使用 ExifInterface 类 可以通过 ExifInterface 类读取图片的旋转角度信息,然后将图片进行相应的旋转。具体的代码如下: ```java ExifInterface exif = new ExifInterface(photoPath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); ``` 2. 使用系统相机调用系统相机拍照时,可以通过设置 Camera.Parameters 属性来控制图片的旋转角度。具体的代码如下: ```java Camera.Parameters parameters = mCamera.getParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; } else { result = (info.orientation - degrees + 360) % 360; } mCamera.setDisplayOrientation(result); parameters.setRotation(result); ``` 以上两种方法都可以解决Android部分手机拍照后获取的图片被旋转问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值