【问题标题】:Front camera is not opening in android platform前置摄像头在安卓平台打不开
【发布时间】:2016-02-03 10:46:53
【问题描述】:

当我在我的应用程序中单击捕获照片图标时,默认情况下它应该打开前置摄像头,但每次它都打开后置摄像头。我需要手动更改它以打开前置摄像头。

     So can anyone please help me out on the above issue.

下面是我打开前置摄像头模式的代码。

    private void getPhotoFromCamera() 
    {
        try 
        {
            PackageManager pm = mContext.getPackageManager();
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           intent.putExtra("android.intent.extras.CAMERA_FACING",1);
            VisitorRegistrationFragment.this.startActivityForResult(intent,FROM_CAMERA);
         } 
         catch (Exception e) 
         {
            e.printStackTrace();
         } 
         catch (OutOfMemoryError e) 
         {
            e.printStackTrace();
         }

    }

【问题讨论】:

标签: android android-intent camera android-camera


【解决方案1】:

改为使用前置摄像头

intent.putExtra("android.intent.extras.CAMERA_FACING",0);

硬编码这个值是个坏主意,应该更好地使用

intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_FRONT)

【讨论】:

  • 我也尝试过上面的代码..但它不工作。以前它工作正常但是当我从 jellybean 升级到 kitkat 版本时,相机总是打开后置摄像头。
【解决方案2】:

这是打开前置摄像头的代码 我的java文件:

public class MainActivity extends Activity {
    int TAKE_PHOTO_CODE = 0;
    public static int count = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Here, we are making a folder named picFolder to store
        // pics taken by the camera using this application.
        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
        File newdir = new File(dir);
        newdir.mkdirs();

        Button capture = (Button) findViewById(R.id.button);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Here, the counter will be incremented each time, and the
                // picture taken by camera will be stored as 1.jpg,2.jpg
                // and likewise.
                count++;
                String file = dir + count + ".jpg";
                File newfile = new File(file);
                   try {
                        newfile.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                Uri outputFileUri = Uri.fromFile(newfile);

                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra("android.intent.extras.CAMERA_FACING",1);
               // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
        }
    }
}

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="aavid.rks.stackovewrflow.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click pic Using Front camera"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="93dp"
        android:layout_marginStart="93dp"
        android:layout_marginTop="133dp" />
</RelativeLayout>

需要两个权限:-

 <uses-permission android:name="android.permission.CAMERA"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【讨论】:

    【解决方案3】:
    private Camera openFrontFacingCameraGingerbread() {
        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    cam = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }
            return cam;
    }
    

    在 AndroidManifest.xml 文件中添加以下权限:

    <uses-permission android:name="android.permission.CAMERA" />   
         <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.front" android:required="false" />
    

    【讨论】:

    • 我已经在清单文件中添加了上述权限..但现在它也打开了后置摄像头
    • 我认为问题出在相机上,因为我已经完成了您在上面评论的所有更改。但仍然无法正常工作
    • 但是使用上面的代码打开了前置摄像头。这对你来说还不够吗?
    【解决方案4】:

    我认为你必须在你的 onCreate() 方法中添加这个:

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
            currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
        } else {
            currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
        }
        camera = Camera.open(currentCameraId);
        camera.setDisplayOrientation(90);
        try {
            camera.setPreviewDisplay(surfaceHolder); 
        } catch (IOException e) {
            e.printStackTrace();
        }
       camera.startPreview();
    

    它对我有用。希望对你也有帮助。

    【讨论】:

      猜你喜欢
      • 2011-02-16
      • 2016-06-06
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多