CameraView не вращает изображение в правильном направлении? - PullRequest
0 голосов
/ 30 сентября 2019

Я использую CameraView внутри приложения. Я хочу получить изображение из CameraView, но оно вращается по часовой стрелке или против часовой стрелки. Это не ведет себя как настоящая камера. Пожалуйста, помогите мне Как CameraView может получить изображение, похожее на реальную камеру телефона, как реальная камера телефона, поворачивает изображение всегда в правильном направлении и показывает изображение в портретном режимеНиже описан мой код.

Класс CameraView

public class CameraActivity extends AppCompatActivity {

    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_activity);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // 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);

        Camera.PictureCallback mPicture = new Camera.PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                FileOutputStream fos =null;
                File picFile = null;
                try {
                    picFile = createImageFile();
                } catch (IOException e) {
                    Timber.e(e,e.getMessage());
                }
                if (picFile == null) {
                    Log.e(Constants.ConstantVariables.TAG, "Couldn't create media file; check storage permissions?");
                    return;
                }
                try {
                    fos = new FileOutputStream(picFile);
                    fos.write(data);
                } catch (Exception e) {
                    Timber.e(e.getMessage(),e);
                }finally {
                    try {
                        if(fos!=null)
                            fos.close();
                    } catch (IOException e) {
                        Timber.e(e, e.getMessage());
                    }
                }
            }
        };

        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // get an image from the camera
                        mCamera.takePicture(null, null, mPicture);
                    }
                }
        );
    }

    /**
     * create image file
     * @return
     * @throws IOException
     */
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = timeStamp;

        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
//        imagePath = image.getAbsolutePath();
        return image;
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); // 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 class CameraActivity extends AppCompatActivity {

    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_activity);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // 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);

        Camera.PictureCallback mPicture = new Camera.PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                FileOutputStream fos =null;
                File picFile = null;
                try {
                    picFile = createImageFile();
                } catch (IOException e) {
                    Timber.e(e,e.getMessage());
                }
                if (picFile == null) {
                    Log.e(Constants.ConstantVariables.TAG, "Couldn't create media file; check storage permissions?");
                    return;
                }
                try {
                    fos = new FileOutputStream(picFile);
                    fos.write(data);
                } catch (Exception e) {
                    Timber.e(e.getMessage(),e);
                }finally {
                    try {
                        if(fos!=null)
                            fos.close();
                    } catch (IOException e) {
                        Timber.e(e, e.getMessage());
                    }
                }
            }
        };

        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // get an image from the camera
                        mCamera.takePicture(null, null, mPicture);
                    }
                }
        );
    }

    /**
     * create image file
     * @return
     * @throws IOException
     */
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = timeStamp;

        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
//        imagePath = image.getAbsolutePath();
        return image;
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); // 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
    }
}

Мой файл minifest.xml

<activity android:name=".ui.CameraActivity"
     android:screenOrientation="portrait"/>

Пожалуйста, помогите мне, я перешел по этой ссылке CameraAPI

...