gpt4 book ai didi

Android 前置摄像头 API

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:15 28 4
gpt4 key购买 nike

我得到了调用另一个具有相机预览的类的 Activity 。问题是它打开了后置摄像头,我需要前置摄像头。默认情况下,我该怎么做,它会打开前置摄像头(我在谷歌中查看,但我尝试过的所有应用程序在打开时都崩溃了)。这是 Activity :

package com.elichai.tfillin;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

private Camera mCamera;
private CameraPreview mPreview;

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

mCamera = getCameraInstance();

mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
}
catch (Exception e){
}
return c;
}

}这是另一个类:

package com.elichai.tfillin;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
int cameraId=0;

@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
mCamera.setDisplayOrientation(90);
if (mHolder.getSurface() == null){
return;
}

try {
mCamera.stopPreview();
} catch (Exception e){
}

try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){
}
}

最佳答案

您当前的 Camera.open() 中没有任何参数,这意味着它将打开几乎总是后置摄像头的默认摄像头。

您应该遍历可用的摄像头并找出前置摄像头的 ID 并使用它打开它。像这样的东西:

private Camera openFrontFacingCamera() 
{
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;
}

然后在您的应用中使用它,如下所示:

public static Camera getCameraInstance() {
Camera c = null;
try {
c = openFrontFacingCamera();
}
catch (Exception e){
}
return c;
}

鉴于这两种方法所做的都是打开并返回 Camera 实例,您可以通过直接调用 openFrontFacingCamera() 而不是 getCameraInstance() 来轻松简化代码。

关于Android 前置摄像头 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14398778/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com