代码之家  ›  专栏  ›  技术社区  ›  Matthew Steinhardt

使用android.hardware.camera2访问摄像头

  •  1
  • Matthew Steinhardt  · 技术社区  · 9 年前

    我想创建一个应用程序,允许我控制相机上的闪光灯,但我对如何做到这一点的研究导致了一系列使用最近贬值的 android.hardware.camera 应用程序编程接口。有人能给我指出正确的方向吗 androird.hardware.camera2 制作一个简单的手电筒应用程序?

    3 回复  |  直到 9 年前
        1
  •  2
  •   josua josh    8 年前

    如果你只想制作一个简单的手电筒应用程序,你应该编辑你的问题。 将相机与新的android.hardware配合使用。camera 2更复杂。

    我曾尝试使用android studio和nexus lg 5x制作相机预览应用程序,但没有成功。

    相反,如果你只是想打开闪光灯,下面是代码:

    void torch(){
        /* turn on the flash light */
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            String[] cameraID = new String[]{};
            cameraID = cameraManager.getCameraIdList();
            for(int i = 0; i<cameraID.length; i++){
                Log.e(TAG,cameraID[i]);
            }
    
            /* camera id is 0 and 1. 0 is the back camera, 1 is the front camera */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                cameraManager.setTorchMode(cameraID[0],true); 
                //true means turned on, false, means turned off.
            }
    
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    
    }
    

    显示:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature android:name="android.hardware.camera2.full" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/MaterialTheme">
    
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
    
    </application>
    

    主要活动.java:

    package com.example.test;
    
    import android.app.Activity;
    import android.graphics.Camera;
    import android.hardware.camera2.params.InputConfiguration;
    import android.os.Build;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
    private final static String TAG = "Camera2testJ";
    private Size mPreviewSize;
    
    private TextureView mTextureView;
    private CameraDevice mCameraDevice;
    private CaptureRequest.Builder mPreviewBuilder;
    private CameraCaptureSession mPreviewSession;
    
    private Button mBtnShot;
    
    private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
    
        mTextureView = (TextureView)findViewById(R.id.texture);
        mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
    
        mBtnShot = (Button)findViewById(R.id.btn_takepicture);
        mBtnShot.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View v) {
                Log.e(TAG, "mBtnShot clicked");
                torch();
            }
    
        });
    
    }
    
    void torch(){
        /* try the flash light */
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);;
        try {
            String[] cameraID = new String[]{};
            cameraID = cameraManager.getCameraIdList();
            for(int i = 0; i<cameraID.length; i++){
                Log.e(TAG,cameraID[i]);
            }
    
            /* camera id is 0 and 1. which one is the front or the back camera? */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                cameraManager.setTorchMode(cameraID[0],true);
            }
    
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    
    }
    

    布局activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextureView
            android:id="@+id/texture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />
    
        <Button
            android:id="@+id/btn_takepicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/picture"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />
    
    </RelativeLayout>
    
        2
  •  1
  •   Rinaldi Segecin    9 年前

    你应该学习 https://github.com/googlesamples/android-Camera2Basic 样品显示了一种控制闪光灯的方法

    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                                        CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
    

    我一直在研究一种方法来获取预览纹理中显示的所有帧,以进行一些图像处理,如果你找到了方法,请告诉我=D。

        3
  •  0
  •   Gowtham Chandrasekaran    9 年前

    android.硬件。照相机已弃用。但你仍然可以利用它,它在棒棒糖和棒棒糖前都有效

    几天前我试过了 http://www.androidsources.com/index.php/2015/09/19/android-flashlight-app-tutorial/