我准备的视图(使用setDisplayOrientation())处于纵向模式,但是生成的图像(使用takePicture()使用SaveImageTask保存)处于横向模式。
这是我的预览&捕获代码:
public void startPreview() {
this.mCamera.setDisplayOrientation(90);
try {
this.mCamera.setPreviewDisplay(this.mSurfaceHolder);
this.mCamera.startPreview();
} catch (Exception e) {
Log.d("camera" , "in startPreview Catch");
}
}
public void captureNow(View view) {
this.mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
this.startPreview();
}
这是我捕获的图像保存代码:
public class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
// Write to SD Card
try {
File dir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "camTest");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
}
如何旋转捕获的图像?似乎传感器始终处于横向模式。(注意:API 21)
答案 0 :(得分:0)
我发布了部分代码,可能会对您有所帮助
File f = new File(path + "image" + imagecount + ".jpg");
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 3;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
null, options);
correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
correctBmp.compress(Bitmap.CompressFormat.PNG, 100,
outstudentstreamOutputStream);
capturedImageIV.setImageBitmap(correctBmp);
} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
答案 1 :(得分:0)
如果要在拍照前更改方向,则必须先配置相机。
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mParameters.setRotation(rotation);
}
AFAIK,您无法在刻录光盘之前更改exif数据。所以,如果你想稍后改变方向,那么你可以通过Arun Antoney的方式做到这一点。