fix: captureScreen() return null
This commit is contained in:
@@ -34,6 +34,7 @@ public class ScreenCapturer {
|
|||||||
private volatile Looper mImageAcquireLooper;
|
private volatile Looper mImageAcquireLooper;
|
||||||
private volatile Image mUnderUsingImage;
|
private volatile Image mUnderUsingImage;
|
||||||
private volatile Image mCachedImage;
|
private volatile Image mCachedImage;
|
||||||
|
private volatile boolean mImageAvailable = false;
|
||||||
private final int mScreenWidth;
|
private final int mScreenWidth;
|
||||||
private final int mScreenHeight;
|
private final int mScreenHeight;
|
||||||
private final int mScreenDensity;
|
private final int mScreenDensity;
|
||||||
@@ -90,6 +91,7 @@ public class ScreenCapturer {
|
|||||||
mCachedImage.close();
|
mCachedImage.close();
|
||||||
}
|
}
|
||||||
mCachedImage = reader.acquireLatestImage();
|
mCachedImage = reader.acquireLatestImage();
|
||||||
|
mImageAvailable = true;
|
||||||
mCachedImageLock.notify();
|
mCachedImageLock.notify();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -100,13 +102,13 @@ public class ScreenCapturer {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Image capture() {
|
public Image capture() {
|
||||||
if (mUnderUsingImage == null && mCachedImage == null) {
|
if (!mImageAvailable) {
|
||||||
waitForImageAvailable();
|
waitForImageAvailable();
|
||||||
}
|
}
|
||||||
if (mCachedImage != null) {
|
synchronized (mCachedImageLock) {
|
||||||
if (mUnderUsingImage != null)
|
if (mCachedImage != null) {
|
||||||
mUnderUsingImage.close();
|
if (mUnderUsingImage != null)
|
||||||
synchronized (mCachedImageLock) {
|
mUnderUsingImage.close();
|
||||||
mUnderUsingImage = mCachedImage;
|
mUnderUsingImage = mCachedImage;
|
||||||
mCachedImage = null;
|
mCachedImage = null;
|
||||||
}
|
}
|
||||||
@@ -116,6 +118,9 @@ public class ScreenCapturer {
|
|||||||
|
|
||||||
private void waitForImageAvailable() {
|
private void waitForImageAvailable() {
|
||||||
synchronized (mCachedImageLock) {
|
synchronized (mCachedImageLock) {
|
||||||
|
if (mImageAvailable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
mCachedImageLock.wait();
|
mCachedImageLock.wait();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ public class Images {
|
|||||||
throw new SecurityException("No screen capture permission");
|
throw new SecurityException("No screen capture permission");
|
||||||
}
|
}
|
||||||
Image capture = mScreenCapturer.capture();
|
Image capture = mScreenCapturer.capture();
|
||||||
if (capture == mPreCapture) {
|
if (capture == mPreCapture && mPreCaptureImage != null) {
|
||||||
return mPreCaptureImage;
|
return mPreCaptureImage;
|
||||||
}
|
}
|
||||||
mPreCapture = capture;
|
mPreCapture = capture;
|
||||||
|
|||||||
Reference in New Issue
Block a user