Skip to content

Commit

Permalink
use real metrics instead size
Browse files Browse the repository at this point in the history
  • Loading branch information
cmzf committed Jan 15, 2020
1 parent 97f438d commit cf8e033
Showing 1 changed file with 7 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
Expand Down Expand Up @@ -35,14 +34,12 @@ public class ScreenCaptureService {
private Handler mHandler;
private Display mDisplay;
private VirtualDisplay mVirtualDisplay;
private int mDensity;
private int mWidth;
private int mHeight;
private int mRotation;
private OrientationChangeCallback mOrientationChangeCallback;
private Activity mAppActivity;
private byte[] mScreenImage;
private int mRequestCode;
private DisplayMetrics mMetrics = new DisplayMetrics();

public static ScreenCaptureService getInstance() {
if (sInstance == null) {
Expand Down Expand Up @@ -80,9 +77,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);

if (mMediaProjection != null) {
// display metrics
DisplayMetrics metrics = mAppActivity.getResources().getDisplayMetrics();
mDensity = metrics.densityDpi;
mDisplay = mAppActivity.getWindowManager().getDefaultDisplay();

// create virtual display depending on device width / height
Expand All @@ -109,15 +103,11 @@ public void stopProjection() {
}

private void createVirtualDisplay() {
// get width and height
Point size = new Point();
mDisplay.getRealSize(size);
mWidth = size.x;
mHeight = size.y;
mDisplay.getRealMetrics(mMetrics);

// start capture reader
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG, mWidth, mHeight, mDensity,
mImageReader = ImageReader.newInstance(mMetrics.widthPixels, mMetrics.heightPixels, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG, mMetrics.widthPixels, mMetrics.heightPixels, mMetrics.densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
Expand All @@ -136,13 +126,13 @@ public void onImageAvailable(ImageReader reader) {
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
int rowPadding = rowStride - pixelStride * mMetrics.widthPixels;

// create bitmap
Bitmap bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(mMetrics.widthPixels + rowPadding / pixelStride, mMetrics.heightPixels, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
// trim black border
bitmap = Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, mMetrics.widthPixels, mMetrics.heightPixels);

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byteStream);
Expand Down

0 comments on commit cf8e033

Please sign in to comment.