@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 onDestroy() { super.onDestroy(); if (mMediaProjection != null) { mMediaProjection.stop(); mMediaProjection = null; } }
private VirtualDisplay createVirtualDisplay() { return mMediaProjection.createVirtualDisplay( "ScreenSharingDemo", mDisplayWidth, mDisplayHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mSurface, null /*Callbacks*/, null /*Handler*/); }
@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(); }
private void createVirtualDisplay() { if (mVirtualDisplay != null) { mVirtualDisplay.release(); } mVirtualDisplay = mMediaProjection.createVirtualDisplay( "ScreenCapture", mWidth, mHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mSurface, null, null); }
// 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); } } }
@CalledByNative public void stopCapture() { Log.d(TAG, "stopCapture"); synchronized (mCaptureStateLock) { if (mMediaProjection != null && mCaptureState == CaptureState.STARTED) { mMediaProjection.stop(); changeCaptureStateAndNotify(CaptureState.STOPPING); } while (mCaptureState != CaptureState.STOPPED) { try { mCaptureStateLock.wait(); } catch (InterruptedException ex) { Log.e(TAG, "ScreenCaptureEvent: " + ex); } } } }
@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); } } }
private void stopProjection() { if (mMediaProjection != null) mMediaProjection.stop(); if (mVirtualDisplay != null) mVirtualDisplay.release(); mImageView.setImageResource(R.mipmap.ic_launcher); }