gpt4 book ai didi

android - 如何显示我的相机预览

转载 作者:行者123 更新时间:2023-11-30 00:47:17 25 4
gpt4 key购买 nike

我写了一个相机预览类

public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
private Camera camera;

public CameraView(Context context) {
super(context);
getHolder().addCallback(this);
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}


public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera){
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation){
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}

int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
}else{
result = (info.orientation - degrees + 360) % 360;
}

camera.setDisplayOrientation(result);

}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
camera = Camera.open();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
setCameraDisplayOrientation((Activity)getContext(), Camera.CameraInfo.CAMERA_FACING_BACK, camera);
Camera.Parameters parameters = camera.getParameters();
Camera.Size bestSize = null;
for (Camera.Size size : parameters.getSupportedPictureSizes()){
if (size.width <= i1 && size.height <=i2) {
if (bestSize == null) {
bestSize = size;
} else {
int bestArea = bestSize.width * bestSize.height;
int newArea = size.width * size.height;

if (newArea > bestArea){
bestSize = size;
}
}
}
}

parameters.setPictureSize(bestSize.width, bestSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);

try{
camera.setPreviewDisplay(surfaceHolder);
}catch (IOException e){
e.printStackTrace();
}

camera.startPreview();


}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
camera = null;
}


}

然后我写了一个布局文件,我想在“SurfaceView”中显示预览

<?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">

<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/camera_view" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:weightSum="1">

<Button
android:id="@+id/stop_navigation"
android:layout_weight="0.5"
android:layout_height="32dp"
android:layout_width="0dp"
android:text="@string/stop_navigation"
android:textColor="@color/colorWhite"
android:textSize="12sp"
android:background="@color/colorAccent" />
</LinearLayout>

</RelativeLayout>

那么在main activity中应该怎么做呢?在我刚刚执行以下代码之前,一切都很好。

CameraView cameraView = new CameraView(this);
addContentView(cameraView, new ActionBar.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT));

但现在我需要将预览放入我设计的布局中。

最佳答案

Use FrameLayout and add CameraPreview to it.



// Create an instance of Camera
mCamera = getCameraInstance();

// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);


/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.

if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}

// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here

// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}


<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>

关于android - 如何显示我的相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41586862/

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