gpt4 book ai didi

android - 表面 View 相机预览

转载 作者:行者123 更新时间:2023-11-30 02:47:15 35 4
gpt4 key购买 nike

在让我的 SurfaceView 显示我的相机预览时遇到一点问题。我在这里查看了一些问题并通过 Google 搜索了一些 tuts,但我认为这可能是我这边的一个小错误,我只是没有看到。

代码

public class RoofPitchActivity extends Activity implements SensorEventListener {
...
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roof_pitch);
initViews();
}

private void initViews() {
...

Preview preview = new Preview(this);
mCamera = Camera.open();
preview.setCamera(mCamera);
}

...
...

class Preview extends ViewGroup implements SurfaceHolder.Callback {

public Preview(Context context) {
super(context);
mSurfaceView = (SurfaceView) findViewById(R.id.preview);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);

mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}

public void setCamera(Camera camera) {
if (mCamera == camera) {
return;
}

stopPreviewAndFreeCamera();

mCamera = camera;

if (mCamera != null) {
requestLayout();

try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}

private void stopPreviewAndFreeCamera() {

if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.startPreview();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
}

因此,当 Activity 启动时,SurfaceView 是黑色的。相机似乎没有将预览渲染到 SurfaceView 上。我确定我遗漏了一些小东西,或者这可能只是对它的工作原理的根本误解。非常感谢有一些新的眼睛和一些解释。谢谢

最佳答案

所以我的问题的答案最终是针对特定设备的。代码是可以接受的,但我用来调试的设备是 Nexus 7。这意味着摄像头是前置的,对摄像头的正常调用不起作用。可以找到关于此的有趣且有趣的读物 in this article

Camera.open();

不起作用。你必须使用

Camera.open(0);

所以我的代码以对相机的条件检查结束,现在它工作正常。

像这样

public void setCamera() {
stopPreviewAndFreeCamera();

PackageManager pm = getPackageManager();
boolean backCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
boolean frontCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);

if (frontCamera) {
mCamera = Camera.open(0);
} else if (backCamera) {
mCamera = Camera.open();
}

if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();

try {
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}

关于android - 表面 View 相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24828735/

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