1. 程式人生 > >Android 獲取螢幕指定座標的顏色

Android 獲取螢幕指定座標的顏色

用到的API

  • MediaProjectionManager
  • MediaProjection
  • VirtualDisplay
  • ImageReader

原理:利用Android系統提供的投影功能把螢幕投影到ImageReader中,通過ImageReader獲取到Bitmap,呼叫BitmapgetPixel(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)

參考