使用相机API拍照时我会遇到照片方向问题,这种情况非常常见并且有一个共同的解决方案:
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;
}
}
但此解决方案无法在所有设备中使用,且图片方向错误。 为了解决这个问题,我删除了第二部分,并在拍照后将旋转校正移动到了:
DECLARE
a CONSTANT NUMBER := 3;
b NUMBER NOT NULL := 5;
BEGIN
:X := :X + a + b + 3;
END;
.
RUN
PRINT :X
这适用于我测试的所有设备,但此解决方案还有另一个问题:需要很长时间,大约3000ms vs 50ms(保存摄像机返回的字节数组时)。有没有解决这个问题的方法。整个方向问题或长时间问题?
答案 0 :(得分:0)
我遇到了同样的问题
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;
}
rotateImage方法:
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);
}
尝试这种方法,我认为它可以帮助你