private void showCamera(int cameraType) {
if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
Toast.makeText(getActivity(), "未检测到SDCard,拍照功能暂时停用",
Toast.LENGTH_LONG).show();
return;
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
long time = currentTimeMillis();
String imgName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(time);
String path = Environment.getExternalStorageDirectory()
+ File.separator + "DCIM" + File.separator + "Camera"
+ File.separator;
File file = new File(path);
if (!file.exists()) {
file.mkdir();
cameraFilePath = path + imgName + ".jpg"; //拍照保存文件路径+文件名称+文件格式
File out = new File(cameraFilePath);
if (!out.exists()) {
try {
out.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
MediaScannerConnection.scanFile(getActivity(),
new String[]{cameraFilePath}, null, null);
startActivityForResult(imageCaptureIntent, cameraType);
重写相机拍摄回调:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == -1) {
switch (requestCode) {
case 102:
//获取拍照路径
//检测照片是否旋转
//是否压缩
//是否保存源文件
String rotatePhoto = PhotoBitmapUtils.amendRotatePhoto(cameraFilePath, getActivity(), false);
break;
default:
break;
} else {
switch (requestCode) {
case 102:
// 取消拍照
File file = new File(cameraFilePath);
if (file.exists()) {
file.delete();
break;
default:
break;
检测方法:
* 处理旋转后的图片
* 默认不压缩
* @param originpath 原图路径
* @param context 上下文
* @param isReplaceFile 是否替换之前的文件 true 替换 false 不替换 默认保存位置
* @return 返回修复完毕后的图片路径
public static String amendRotatePhoto(String originpath, Context context, boolean isReplaceFile) {
return amendRotatePhoto(originpath, context, false, isReplaceFile);
* 处理旋转后的图片
* 默认不压缩
* 默认替换原图路径下保存
* @param originpath
* @param context
* @return
public static String amendRotatePhoto(String originpath, Context context) {
return amendRotatePhoto(originpath, context, false, true);
* 处理旋转后的图片
* @param originpath 原图路径
* @param context 上下文
* @param isCompress 是否压缩
* @param isReplaceFile 是否替换之前的文件 true 替换 false 不替换 默认保存位置
* @return 返回修复完毕后的图片路径
public static String amendRotatePhoto(String originpath, Context context, boolean isCompress, boolean isReplaceFile) {
if (TextUtils.isEmpty(originpath)) return originpath;
// 取得图片旋转角度
int angle = readPictureDegree(originpath);
//是否压缩
Bitmap bmp = null;
if (isCompress) {
// 把原图压缩后得到Bitmap对象
bmp = getCompressPhoto(originpath);
if (bmp != null) {
//处理旋转
Bitmap bitmap = null;
if (angle != 0) {
// 修复图片被旋转的角度
bitmap = rotaingImageView(angle, bmp);
if (bitmap != null) {
// 保存修复后的图片并返回保存后的图片路径
return savePhotoToSD(bitmap, originpath, context, isReplaceFile);
} else {
Bitmap localBitmap = getLocalBitmap(originpath);
if (localBitmap == null) return originpath;
//处理旋转
Bitmap bitmap = null;
if (angle != 0) {
// 修复图片被旋转的角度
bitmap = rotaingImageView(angle, localBitmap);
if (bitmap != null) {
return savePhotoToSD(bitmap, originpath, context, isReplaceFile);
} else {
return originpath;
* 读取照片旋转角度
* @param path 照片路径
* @return 角度
public static int readPictureDegree(String path) {
int degree = 0;
try {
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;
/* 旋转图片
* @param angle 被旋转角度
* @param bitmap 图片对象
* @return 旋转后的图片
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
if (returnBm == null) {
returnBm = bitmap;
if (bitmap != returnBm) {
bitmap.recycle();
return returnBm;
* 保存Bitmap图片在SD卡中
* 如果没有SD卡则存在手机中
* @param mbitmap 需要保存的Bitmap图片
* @param originpath 文件的原路径
* @param isReplaceFile 是否替换原文件
* @return 保存成功时返回图片的路径,失败时返回null
public static String savePhotoToSD(Bitmap mbitmap, String originpath, Context context, boolean isReplaceFile) {
FileOutputStream outStream = null;
String fileName = "";
if (mbitmap == null) return originpath;
if (isReplaceFile) {
fileName = getPhotoFileName(context);
} else {
if (TextUtils.isEmpty(originpath)) return originpath;
fileName = originpath;
try {
outStream = new FileOutputStream(fileName);
// 把数据写入文件,100表示不压缩
mbitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
return fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (outStream != null) {
// 记得要关闭流!
outStream.close();
if (mbitmap != null) {
mbitmap.recycle();
} catch (Exception e) {
e.printStackTrace();
https://blog.csdn.net/zhuwentao2150/article/details/51999908
在调用系统相机拍照的时候突在某些机型上发现拍出来的照片有可能已经不是原来的角度 不是旋转了90度就是旋转了180度,为了适配这个问题下面是一波套路、、、调用系统相机:/** * 打开系统相机记录成长图片 */private void showCamera(int cameraType) { if (!Environment.MEDIA_MOUNTED.equal...
开发过Android自定义相机的朋友们估计都被相机的各种乱七八糟的旋转角度适配坑过,本文将对Camera的各种角度进行解析。
一、适配目标
根据相机旋转角度以及屏幕显示旋转角度选择相机预览数据显示到View上的预览数据显示旋转角度,使眼睛直接看到的真实画面和手机屏幕中显示的画面效果相同。
**相机旋转角度:**相机成像相对于手机的旋转角度,若设备已经安装上了相机,那么该相机相对于设备的旋转角度...
最近用Android camera2做自定义相机,基本的关于打开相机到预览的过程就不多叙述了
大家参看https://github.com/gengqifu/361Camera这个文章就可以知道
今天详细讲讲我开过程中遇到的旋转角度的问题
直接上代码
//初始化传感器定位
orientationEventListener = object : OrientationEventListener(mActivity) {
override fun onOrientationC
找到最终保存照片的方法 saveDataToStorage()
vendor\mediatek\proprietary\packages\apps\Camera2\common\src\com\mediatek\camera\common\storage\MediaSaver.java
增加 rotatePicture() 方法
private void saveDataToStorage(Request request) {
LogHelper.d(TAG, "[saveDataToSt
Layout:activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:la...
这几天犯了一个错误,初期想着甩锅给后台的,但是还好及时发现了是自身的问题~
产生问题:通过拍照或相册回传的照片,直接传入后台,在用于展示的时候,照片角度出现问题,均不规则的旋转了90度,从而导致体验效果变差!
问题思考:后台一般都是你怎么存的,它怎么给你拿出来!所以在这里就可以发现问题本身是由我们前端造成的!
覆盖范围:图片被旋转的情况并不是百分百出现在每个机型上,只是会出现在某一部分机型之...
1.在AndroidManifest.xml文件中添加相机权限
<uses-permission android:name="android.permission.CAMERA"/>
2.创建一个ImageView组件用于显示拍照后的照片
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3.在项目中创建一个java类来处理拍照的逻辑
private void takePhoto(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "takePhoto: " + ex.getMessage());
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider",photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
4.在onActivityResult中获取拍照结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);