您的位置:首页 > 移动开发 > Android开发

Android 获取屏幕指定坐标的颜色

2017-12-24 13:04 3059 查看

用到的API

MediaProjectionManager


MediaProjection


VirtualDisplay


ImageReader


原理:利用Android系统提供的投影功能把屏幕投影到
ImageReader
中,通过
ImageReader
获取到
Bitmap
,调用
Bitmap
getPixel(x, y)
方法获取到指定坐标的颜色。

代码

创建虚拟显示器

private static final int REQUEST_MEDIA_PROJECTION = 1;
private MediaProjectionManager mMediaProjectionManager;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

assert mMediaProjectionManager != null;
startActivityForResult(
mMediaProjectionManager.createScreenCaptureIntent(),
REQUEST_MEDIA_PROJECTION);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION) {
if (resultCode != Activity.RESULT_OK) {
Log.i(TAG, "User cancelled");
Toast.makeText(this, "User cancelled!", Toast.LENGTH_SHORT).show();
return;
}

Log.i(TAG, "Starting screen capture");

mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
setUpVirtualDisplay();
}
}

private void setUpVirtualDisplay() {
Point size = new Point();
DisplayMetrics metrics = new DisplayMetrics();
Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
defaultDisplay.getSize(size);
defaultDisplay.getMetrics(metrics);

final ImageReader imageReader = ImageReader.newInstance(size.x, size.y, PixelFormat.RGBA_8888, 1);
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
size.x, size.y, metrics.densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
imageReader.getSurface(), null, null);

GBData.reader = imageReader;
}


获取指定坐标的颜色

public class GBData {
private static final String TAG = "GBData";
static ImageReader reader;
private static Bitmap bitmap;

public static int getColor(int x, int y) {
if (reader == null) {
Log.w(TAG, "getColor: reader is null");
return -1;
}

Image image = reader.acquireLatestImage();

if (image == null) {
if (bitmap == null) {
Log.w(TAG, "getColor: image is null");
return -1;
}
return bitmap.getPixel(x, y);
}
int width = image.getWidth();
int height = image.getHeight();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
}
bitmap.copyPixelsFromBuffer(buffer);
image.close();

return bitmap.getPixel(x, y);
}
}


在代码中使用

int color = GBData.getColor(x,y)


参考

https://github.com/googlesamples/android-ScreenCapture/#readme

https://developer.android.com/reference/android/media/ImageReader.html#getSurface()

https://www.jianshu.com/p/d7eb518195fd
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 坐标颜色