[英]camera picture orientation in a camera app
使用相機API拍照時,我遇到圖片定位問題,這很常見,並且有一個常見的解決方案:
mRotation = getRotation();
mCamera.setDisplayOrientation(mRotation);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setRotation(mRotation);
mCamera.setParameters(parameters);
但是此解決方案不適用於所有設備,並且圖片的方向錯誤。 為了解決這個問題,我在拍照后刪除了第二部分並將旋轉校正移至:
private static class SaveImageTask extends AsyncTask<byte[], Void, Void>
{
private WeakReference<CameraActivity> mActivityWeakReference;
SaveImageTask(CameraActivity cameraActivity)
{
mActivityWeakReference = new WeakReference<>(cameraActivity);
}
@Override
protected Void doInBackground(byte[]... params)
{
CameraActivity cameraActivity = mActivityWeakReference.get();
if (cameraActivity == null || cameraActivity.isFinishing())
return null;
Bitmap source = BitmapFactory.decodeByteArray(params[0], 0, params[0].length);
if (source == null)
{
Toast.makeText(cameraActivity, "Captured image is empty", Toast.LENGTH_LONG).show();
return null;
}
Matrix matrix = new Matrix();
matrix.postRotate(cameraActivity.mCameraPreview.getPictureRotation());
Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
File rootDir = new File(cameraActivity.getFilesDir(), cameraActivity.mNote.getId().toString());
rootDir.mkdir();
File imageFolder = new File(rootDir, StringUtils.getImagePath());
imageFolder.mkdir();
long time = System.currentTimeMillis();
File imageName = new File(imageFolder, StringUtils.getImageFilePath(String.valueOf(time)));
FileOutputStream outStream;
try
{
outStream = new FileOutputStream(imageName);
//outStream.write(params[0]);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return null;
}
}
該方法適用於我測試過的所有設備,但是此解決方案還有另一個問題:需要較長時間,大約3000毫秒和50毫秒(保存相機返回的字節數組時)。 有沒有解決這個問題的辦法。 整個取向問題還是長期問題?
我也遇到過同樣的問題
ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
}
rotationImage方法:
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
source.getHeight(),
matrix, true);
}
嘗試這種方法,我認為它可以幫助您
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.