現象
- 魅族 MX2 唯一支援的
PictureSize 為 1280x720。
- 專案代碼寫死
parameters.setPictureSize(640, 480)。
- 結果:相機 HAL 忽略無效尺寸,回退預設值,導致輸出畫面旋轉、裁切異常。
修正代碼
Camera.Parameters params = mCamera.getParameters();
List supportedSizes = params.getSupportedPictureSizes();
// 策略 1:優先使用 1280x720 (已知 MX2 唯一支援)
Camera.Size targetSize = null;
for (Camera.Size size : supportedSizes) {
if (size.width == 1280 && size.height == 720) {
targetSize = size;
break;
}
}
// 策略 2:若未來機型支援更多,選最接近 4:3 或 16:9 的最大尺寸
if (targetSize == null) {
for (Camera.Size size : supportedSizes) {
if (targetSize == null || size.width * size.height > targetSize.width * targetSize.height)
targetSize = size;
}
}
if (targetSize != null) {
params.setPictureSize(targetSize.width, targetSize.height);
}
mCamera.setParameters(params);
通用最佳實踐
- **永不寫死尺寸**,必須遍歷
getSupportedPictureSizes() 選擇。
- 預覽尺寸
PreviewSize 同理,需從 getSupportedPreviewSizes() 選擇。
- 方向處理:
setDisplayOrientation(90) 僅影響預覽,**不影響** 存檔照片方向。存檔需在 onPictureTaken 中根據 ExifInterface.ORIENTATION 旋轉 Bitmap。
- 測試機型覆蓋:至少覆蓋主流廠商 (三星、小米、華為、OPPO、vivo、魅族) 各 1-2 機型。