简体   繁体   English

将相机方向锁定为人像

[英]Lock Camera orientation to portrait

How can I prevent the android camera to switch orientation to landscape if I start the camera like this: 如果我像这样启动相机,如何防止android相机将方向切换为横向:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imageFolder = new File (Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
    imageFile = new File (imageFolder, UUID.randomUUID().toString()+".png");
    Uri uriImage = Uri.fromFile(imageFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
    startActivityForResult(intent, 0);

So I don't have a particular camera activity. 所以我没有特定的相机活动。 In the manifest.xml the whole appliaction is set to portrait orientation, but the camera switches. 在manifest.xml中,整个应用设置为纵向,但相机会切换。

Second Problem, after taking picture and setting it to the imageView it ist switched in orientatien even if I took it in portrait mode (I save the image before I set the imageView), how can I display it in the right posititon? 第二个问题,在拍照并将其设置为imageView后,即使以人像模式拍摄(即使在设置imageView之前保存图像),它也会以定向方式切换,如何在正确的位置显示呢?

try this for lock camera orientation to portrait, 尝试将相机锁定为纵向,

// *************************************************************************//
    // Stop the screen orientation changing during an event
    // *************************************************************************//

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
    }

    private void lockScreenRotation(int orientation)
    {
        // Stop the screen orientation changing during an event
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    // *************************************************************************//
    // store the file url as it will be null after returning from camera app
    // *************************************************************************//

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        // save file url in bundle as it will be null on scren orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

Set this on onActivityResult for getting picture in right position 在onActivityResult上设置此选项可将图片放置在正确的位置

if (resultCode == Activity.RESULT_OK
                && requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
        {
            filePath = fileUri.getPath();

            Utility.log("FILE PATH CAMEREA:->", filePath);
            // AppSharedPrefrence.getInstance(getActivity()).setImagePath(path);

            // *************************************************************************//
            // set default camera rotation
            // *************************************************************************//
            try
            {
                File f = new File(filePath);
                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 = 2;

                Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                Bitmap bmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);
                OutputStream stream = new FileOutputStream(filePath);
                bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);

                // imageBmp =
                // Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path),
                // 400, 400, true);
                imageBmp = BitmapFactory.decodeFile(filePath);
                imgProfilePic.setImageBitmap(imageBmp);

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (OutOfMemoryError oom)
            {
                oom.printStackTrace();
            }

        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM