ボタンを押すと、写真を撮って保存するアクティビティ(結果用)を起動するアプリケーションを開発しています。前のアクティビティより写真が表示されます。私はそれを達成するためにCameraPreview
クラスとCameraActivity
アクティビティを使用しています。すべてが縦向きモードである必要があり、すべて完全に機能します。プレビューで新しいアクティビティを開始し、縦向きモードで写真を撮り、ボックスに表示された写真で前のアクティビティに戻ります。
唯一の問題は、画像が反時計回りに 90° 回転して表示 (および保存) されることです。どうすれば回避できますか?
ここに私の活動があります:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Create an instance of Camera
mCamera = getCameraInstance();
mCamera.setDisplayOrientation(90);
Parameters params = mCamera.getParameters();
params.setRotation(90);
params.setPictureSize(100, 200);
mCamera.setParameters(params);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions");
Intent intent = getIntent();
setResult(RESULT_CANCELED, intent);
finish();
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Intent intent = getIntent();
setResult(RESULT_OK, intent);
finish();
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
return;
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
Intent intent = getIntent();
setResult(RESULT_CANCELED, intent);
finish();
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
return;
}
};
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
public void scatta(View v) {
mCamera.takePicture(null, null, mPicture);
}
private File getOutputMediaFile(){
HERE I RETURN A VALID FILE.
return file;
}
}
プレビューは次のとおりです。
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
mCamera.setDisplayOrientation(90);
Parameters params = mCamera.getParameters();
params.setRotation(90);
mCamera.setParameters(params);
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
ありがとうございました