Exemplo n.º 1
0
  @CalledByNative
  public void startCapture() {
    Log.d(TAG, "startCapture");
    synchronized (mCaptureStateLock) {
      if (mCaptureState != CaptureState.ALLOWED) {
        Log.e(TAG, "startCapture() invoked without user permission.");
        return;
      }
    }
    mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);
    if (mMediaProjection == null) {
      Log.e(TAG, "mMediaProjection is null");
      return;
    }
    mMediaProjection.registerCallback(new MediaProjectionCallback(), null);

    mThread = new HandlerThread("ScreenCapture");
    mThread.start();
    mBackgroundHandler = new Handler(mThread.getLooper());

    // On Android M and above, YUV420 is prefered. Some Android L devices will silently
    // fail with YUV420, so keep with RGBA_8888 on L.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      mFormat = PixelFormat.RGBA_8888;
    } else {
      mFormat = ImageFormat.YUV_420_888;
    }
    maybeDoRotation();
    createImageReaderWithFormat();
    createVirtualDisplay();

    changeCaptureStateAndNotify(CaptureState.STARTED);
  }
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode != PERMISSION_CODE) {
     Log.e(TAG, "Unknown request code: " + requestCode);
     return;
   }
   if (resultCode != RESULT_OK) {
     Toast.makeText(this, "User denied screen sharing permission", Toast.LENGTH_SHORT).show();
     return;
   }
   mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
   mMediaProjection.registerCallback(new MediaProjectionCallback(), null);
   mVirtualDisplay = createVirtualDisplay();
 }
Exemplo n.º 3
0
  // region Media Projection
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
      mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);

      if (mMediaProjection != null) {
        store_directory =
            Environment.getExternalStorageDirectory().getAbsolutePath() + "/MediaProjection/";
        File storeDirectory = new File(store_directory);
        if (!storeDirectory.exists()) {
          storeDirectory.mkdirs();
          if (!storeDirectory.isDirectory()) {
            Log.e(TAG, "failed to create file storage directory.");
            return;
          }
        }

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int density = metrics.densityDpi;
        int flags =
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
                | DisplayManager
                    .VIRTUAL_DISPLAY_FLAG_PUBLIC; // DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        mWidth = size.x;
        mHeight = size.y;

        mImageReader =
            ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888 /*ImageFormat.JPEG*/, 2);
        mImageReader.setOnImageAvailableListener(new TestImageAvailableListener(), null);
        mVirtualDisplay =
            mMediaProjection.createVirtualDisplay(
                "screencap",
                mWidth,
                mHeight,
                density,
                flags,
                mImageReader.getSurface(),
                mVirtualDisplayCallback,
                null);
      }
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {

      mediaProjection = projectionManager.getMediaProjection(resultCode, data);
      if (mediaProjection != null) {

        projectionStarted = true;

        // Initialize the media projection
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int density = metrics.densityDpi;
        int flags =
            DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
                | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        displayWidth = size.x;
        displayHeight = size.y;

        imageReader =
            ImageReader.newInstance(displayWidth, displayHeight, PixelFormat.RGBA_8888, 2);
        mediaProjection.createVirtualDisplay(
            "screencap",
            displayWidth,
            displayHeight,
            density,
            flags,
            imageReader.getSurface(),
            null,
            handler);
        imageReader.setOnImageAvailableListener(new ImageAvailableListener(), handler);
      }
    }
  }