2017-03-27 58 views
1

我需要在動態壁紙中顯示相機預覽。 我已經爲android < M版本製作了。但無法理解它如何使用新的API。現在在壁紙服務中顯示相機2預覽

最古老的相機已被棄用在谷歌例如,它們把它在XML和TextureView但如何改變我的需要,我無法理解。

無論如何謝謝!

谷歌例如 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); 

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類

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

回答

0

您只需繼續使用舊的API;即使在新設備上它也是完全有效的。

否則,只需更換一個SurfaceView容易夠TextureView;而不是從TextureView的SurfaceTexture創建Surface,從surfaceChanged中的SurfaceView的SurfaceHolder中獲取Surface。

+0

舊的API沒有在我的Android 5.0設備上工作5.0仍在工作 – Joao