Search code examples
javaandroidandroid-studiocameracamera2

Show camera2 preview in wallpaperservice


I need to show camera preview in live wallpaper. I've already made for android < M versions. But can't understand how it working with new API. Oldest Camera is deprecated now

In google example they put it in xml and in TextureView but how I can change that for my needs, I can't understand.

Anyway Thanks!

Google example https://github.com/googlesamples/android-Camera2Basic

Below my code.

Start wallpaper

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

TransparentWallpaperService

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);
        }
    }
}
}

CameraView 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;
}
}

Solution

  • You can just keep using the old API; it's fully functional even on new devices.

    Otherwise, you can just replace the TextureView with a SurfaceView easily enough; instead of creating a Surface from TextureView's SurfaceTexture, get a Surface from SurfaceView's SurfaceHolder in surfaceChanged.