使用新 API 21 获取相机预览
Get Camera Preview with new API 21
我想重复使用 class 'CameraPreview' 我在网上找到的一个旧项目,现在我想再次使用那个 class hovewer 旧的 class相机已被弃用,建议使用 this。不幸的是,他们已经更改了所有方法,我找不到用这种新方法更正我的 class 的方法。有谁知道吗?
class 相机预览:
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder mHolder;
private Camera mCamera;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;
public CameraPreview(Context context, Camera camera,PreviewCallback previewCb,AutoFocusCallback autoFocusCb)
{
super(context);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
// Set camera to continuous focus if supported, otherwise use
// software auto-focus. Only works for API level >=9.
// Camera.Parameters parameters = camera.getParameters();
// for (String f : parameters.getSupportedFocusModes()) {
// if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
// mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// autoFocusCallback = null;
// break;
// }
// }
// 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);
} catch (IOException e) {
Log.d("DBG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// Camera preview released in activity
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
/*
* 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
}
try {
// Hard code camera surface rotation 90 degs to match Activity view in portrait
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallback(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e){
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
}
新的 camera2 API 有一个完全不同的工作方式,所以我建议你看看 GitHub 上可用的基本示例:
https://github.com/googlesamples/android-Camera2Basic
您也不必切换到新的 API - 旧的 android.hardware.Camera API 已弃用,但会像以前一样继续工作。它最终会被删除,但要过一段时间才会发生。
我想重复使用 class 'CameraPreview' 我在网上找到的一个旧项目,现在我想再次使用那个 class hovewer 旧的 class相机已被弃用,建议使用 this。不幸的是,他们已经更改了所有方法,我找不到用这种新方法更正我的 class 的方法。有谁知道吗?
class 相机预览:
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder mHolder;
private Camera mCamera;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;
public CameraPreview(Context context, Camera camera,PreviewCallback previewCb,AutoFocusCallback autoFocusCb)
{
super(context);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
// Set camera to continuous focus if supported, otherwise use
// software auto-focus. Only works for API level >=9.
// Camera.Parameters parameters = camera.getParameters();
// for (String f : parameters.getSupportedFocusModes()) {
// if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
// mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// autoFocusCallback = null;
// break;
// }
// }
// 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);
} catch (IOException e) {
Log.d("DBG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// Camera preview released in activity
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
/*
* 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
}
try {
// Hard code camera surface rotation 90 degs to match Activity view in portrait
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallback(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e){
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
}
新的 camera2 API 有一个完全不同的工作方式,所以我建议你看看 GitHub 上可用的基本示例:
https://github.com/googlesamples/android-Camera2Basic
您也不必切换到新的 API - 旧的 android.hardware.Camera API 已弃用,但会像以前一样继续工作。它最终会被删除,但要过一段时间才会发生。