找到最终保存照片的方法 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, "[saveDataToStorage]+"); if (request.mData == null) { LogHelper.w(TAG, "data is null,return!"); return; } if (request.mFilePath == null && request.mValues != null) { LogHelper.d(TAG, "get filePath from contentValues."); request.mFilePath = request.mValues.getAsString(ImageColumns.DATA); } if (request.mFilePath == null) { LogHelper.w(TAG, "filePath is null, return"); return; } String tempFilePath = request.mFilePath + TEMP_SUFFIX; if (request.mType == HeifHelper.FORMAT_HEIF) { int width = request.mValues.getAsInteger(ImageColumns.WIDTH).intValue(); int height = request.mValues.getAsInteger(ImageColumns.HEIGHT).intValue(); int orientation = request.mValues.getAsInteger(ImageColumns.ORIENTATION).intValue(); HeifHelper.saveData(request.mData, width, height, orientation, request.mFilePath); LogHelper.d(TAG, "[saveDataToStorage]-"); return; } FileOutputStream out = null; try { // Write to a temporary file and rename it to the final name. // This // avoids other apps reading incomplete data. LogHelper.d(TAG, "save the data to SD Card"); out = new FileOutputStream(tempFilePath); out.write(request.mData); out.close(); new File(tempFilePath).renameTo(new File(request.mFilePath)); rotatePicture(request.mFilePath); } catch (IOException e) { LogHelper.e(TAG, "Failed to write image,ex:", e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { LogHelper.e(TAG, "IOException:", e); } } } LogHelper.d(TAG, "[saveDataToStorage]-"); } private void rotatePicture(String mFilePath){ LogHelper.d(TAG, "[rotatePicture]-"+mFilePath); Bitmap picture = BitmapFactory.decodeFile(mFilePath); Matrix matrix = new Matrix(); matrix.postRotate(270);\\旋转 matrix.postScale(-1, 1);\\水平镜像 Bitmap resizeBitmap = Bitmap.createBitmap(picture, 0, 0, picture.getWidth(), picture.getHeight(), matrix, true); File file = new File(mFilePath); java.io.OutputStream outputStream = null; try { outputStream = new FileOutputStream(file); resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } }