【问题标题】:Lock Camera orientation to portrait将相机方向锁定为纵向
【发布时间】:2015-06-08 01:44:02
【问题描述】:

如果我这样启动相机,如何防止 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);

所以我没有特定的相机活动。在 manifest.xml 中,整个应用程序设置为纵向,但相机会切换。

第二个问题,在拍照并将其设置为 imageView 后,即使我以纵向模式拍摄(我在设置 imageView 之前保存了图像),它仍会在 orientatien 中切换,如何将其显示在正确的位置?

【问题讨论】:

    标签: android camera orientation


    【解决方案1】:

    尝试将相机方向锁定为纵向,

    // *************************************************************************//
        // 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");
        }
    

    在 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();
                }
    
            } 
    

    【讨论】:

    • filePath是什么类型的变量?
    • 字符串是图片文件的路径
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多