如何将相机预览设置为壁纸?

时间:2014-01-03 07:10:12

标签: android camera android-camera live-wallpaper wallpaper

我有一个项目,当用户点击按钮时我有一个按钮,然后将相机预览设置为壁纸,任何人都可以告诉我该怎么做?

我正在使用以下代码进行相机预览

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

private SurfaceHolder mHolder;
private Camera mCamera;


public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera=camera;
    // TODO Auto-generated constructor stub
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("DEBUG", "Error setting camera preview: " + e.getMessage());
    }
}



@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

    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("DEBUG", "Error starting camera preview: " + e.getMessage());
    }
}
}

但我的问题是如何将相机预览添加到WallpaperService?

2 个答案:

答案 0 :(得分:0)

首先,当用户点击按钮时,拍摄相机预览图片并找到保存图片的路径

然后参考此链接,通过指定路径将特定图像设置为墙纸。

How to set image as wallpaper programmatically?

答案 1 :(得分:0)

您需要使用WallpaperService创建动态壁纸。然后,您需要让您的服务创建自己的渲染表面,以便将相机预览绘制到其中。