1

我正在使用MediaProjectionAPI 捕获我的 Android 设备屏幕,方法是创建一个VirtualDisplay并将其显示在SurfaceView始终可见的 a 上。我想对 VirtualDisplay 抓取的帧进行一些图像处理,然后将它们实时绘制回 SurfaceView。有没有办法使用 VirtualDisplay 和 MediaProjection API 来做到这一点?

RecordingSession.java

class RecordingSession implements SurfaceHolder.Callback {
  static final int VIRT_DISPLAY_FLAGS=
    DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
      DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
  private RecordingConfig config;
  private final Context ctxt;
  private final ToneGenerator beeper;
  private MediaRecorder recorder;
  private MediaProjection projection;
  private VirtualDisplay vdisplay;
  private Window window;
  private Bitmap latestBitmap=null;

  RecordingSession(Context ctxt, RecordingConfig config,
                   MediaProjection projection, Window window) {
    this.ctxt=ctxt.getApplicationContext();
    this.window = window;
    this.config=config;
    this.projection=projection;
    this.beeper=new ToneGenerator(
      AudioManager.STREAM_NOTIFICATION, 100);
  }

  void start() {
    this.window = new Window(this.ctxt);
    this.window.open();
    this.window.getScreenShot().getHolder().addCallback(this);
    vdisplay=projection.createVirtualDisplay("andcorder",
            config.width*4, config.height*4, config.density,
            VIRT_DISPLAY_FLAGS, null, null, null);
    beeper.startTone(ToneGenerator.TONE_PROP_ACK);
  }

  void stop() {
    projection.stop();
    vdisplay.release();
    beeper.startTone(ToneGenerator.TONE_PROP_NACK);
  }

  @Override
  public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {

  }

  @Override
  public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    vdisplay.setSurface(surfaceHolder.getSurface());
  }

  @Override
  public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

  }

  public void end() {
    this.window.close();
  }
}
4

0 回答 0