在 wallpaperservice 中显示 camera2 预览

Show camera2 preview in wallpaperservice

我需要在动态壁纸中显示相机预览。 我已经制作了 android < M 版本。但无法理解它如何与新 API 一起工作。最旧的相机现已弃用

在 google 示例中,他们将其放在 xml 和 TextureView 中,但我无法理解如何根据需要更改它。

无论如何谢谢!

Google 例子 https://github.com/googlesamples/android-Camera2Basic

下面是我的代码。

开始壁纸

Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, TransparentWallpaperService.class));
startActivity(intent);

透明壁纸服务

public class TransparentWallpaperService extends WallpaperService {

@Override
public Engine onCreateEngine() {
    return new GlowEngine();
}

private class GlowEngine extends Engine {

    private final Handler handler = new Handler();

    private final Runnable viewRunner = new Runnable() {

        @Override
        public void run() {

            drawView();

        }

    };

    private boolean visible = true;
    private CameraView view;

    public GlowEngine() {
        super();
        view = new CameraView(getBaseContext(), getSurfaceHolder());
        handler.post(viewRunner);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visible = visible;
        if (visible) {
            handler.post(viewRunner);
        }
        else {
            handler.removeCallbacks(viewRunner);
        }
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        this.visible = false;
        handler.removeCallbacks(viewRunner);
    }

    private void drawView() {
        view.surfaceChanged(getSurfaceHolder(), OPAQUE, view.getWidth(), view.getHeight());
        handler.removeCallbacks(viewRunner);
        if (visible) {
            handler.postDelayed(viewRunner, 4000);
        }
    }
}
}

相机视图class

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holder;

private Camera camera;

public CameraView(Context context) {

    super(context);

    camera = getCameraInstance();

}

public CameraView(Context context, SurfaceHolder holder) {

    this(context);
    this.holder = holder;

    holder.addCallback(this);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {

    if(camera == null) {
        camera = getCameraInstance();
    }

    if(camera != null) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        }
        catch (IOException e) {
            Log.e("CameraView", "Error setting camera preview: " + e.getMessage());
        }
    }

}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {

    boolean portrait = true;

    if (width > 0 && height >0 && width < height){
        portrait =true;
    } else if (width > 0 && height >0 && width > height){
        portrait = false;
    }

    Camera.Parameters parameters;
    if (camera == null) {
        camera = getCameraInstance();
    }
    if (camera != null){
        parameters = camera.getParameters();
        Camera.Size size = parameters.getPictureSize();
        size = parameters.getPreviewSize();
        parameters.setPreviewSize(size.width, size.height);
        if (portrait) {
            camera.setDisplayOrientation(90);
        } else {
            camera.setDisplayOrientation(180);
        }
        try {
            camera.setParameters(parameters);
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    if(camera != null) {
        try {
            camera.stopPreview();
            camera.release();
        }
        catch (Exception e) {
            Log.e("CameraView", "Error stopping camera preview: " + e.getMessage());
        }
    }

}

private Camera getCameraInstance() {

    Context context = getContext();
    Camera camera = null;
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        try {
            camera = Camera.open();
        }
        catch (Exception e) {
            Log.e("CameraView", "Error getting camera instance: " + e.getMessage());
        }
    }
    else {
        Log.i("CameraView", "No camera found!");
    }
    return camera;
}
}

您可以继续使用旧的 API;即使在新设备上也能正常使用。

否则,您只需将 TextureView 替换为 SurfaceView 即可;不是从 TextureView 的 SurfaceTexture 创建 Surface,而是从 SurfaceChanged 中的 SurfaceView 的 SurfaceHolder 获取 Surface。