通过Socket发送输出Mediaprojection API android

时间:2016-04-12 21:04:55

标签: android sockets android-bitmap android-mediaprojection

我想通过Socket分享android的屏幕,android的版本无所谓。

经过大量搜索我发现了什么: MediaProjection.createVirtualDisplay到ImageReader到Bitmap并通过Socket Network发送。你觉得怎么样?

它给出一个黑框并出现此错误:

Attempt to invoke virtual method 'boolean android.os.Handler.post(java.lang.Runnable)' on a null object reference

我走过了thisthisthis

我在这里定义ImageReader:

Point size = new Point();
        mDisplay.getSize(size);
        mWidth = size.x;
        mHeight = size.y;

mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
            mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
            mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);

ImageReader到位图:

private class ImageAvailableListener implements ImageReader.OnImageAvailableListener {
        @Override
        public void onImageAvailable(ImageReader reader) {
            try {
                Image image = mImageReader.acquireLatestImage();
                final Image.Plane[] planes = image.getPlanes();
                final ByteBuffer buffer = planes[0].getBuffer();
                int offset = 0;
                int pixelStride = planes[0].getPixelStride();
                int rowStride = planes[0].getRowStride();
                int rowPadding = rowStride - pixelStride * mWidth;
                Bitmap bitmap = Bitmap.createBitmap(mWidth+rowPadding/pixelStride, mHeight, Bitmap.Config.ARGB_8888);
                bitmap.copyPixelsFromBuffer(buffer);
                image.close();
                if(bitmap!=null)
                    new Thread(new SendMessage(bitmap)).start();
}catch (Exception e) {
            e.printStackTrace();
        }

我也尝试了这个,但它不起作用:

 int width = img.getWidth();
                    int height = img.getHeight();
                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * width;
                    byte[] newData = new byte[width * height * 4];

                    int offset = 0;
                    bitmap = Bitmap.createBitmap(metrics,width, height, Bitmap.Config.ARGB_8888);
                    ByteBuffer buffer = planes[0].getBuffer();
                    for (int i = 0; i < height; ++i) {
                        for (int j = 0; j < width; ++j) {
                            int pixel = 0;
                            pixel |= (buffer.get(offset) & 0xff) << 16;     // R
                            pixel |= (buffer.get(offset + 1) & 0xff) << 8;  // G
                            pixel |= (buffer.get(offset + 2) & 0xff);       // B
                            pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
                            bitmap.setPixel(j, i, pixel);
                            offset += pixelStride;
                        }
                        offset += rowPadding;
                    }

通过Socket发送位图,如果我从drawable读取位图,它就可以工作:

 private class SendMessage implements Runnable{
        Bitmap image;
        public SendMessage( Bitmap im) {

            image = im;
        }

        public void run() {

            Socket socket = null;
            DataOutputStream dataOutputStream = null;
            DataInputStream dataInputStream = null;

            try {
                socket = new Socket("********", 8888);

                image.compress(Bitmap.CompressFormat.JPEG,50, socket.getOutputStream());



            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

欢迎任何建议,谢谢 如果您有其他方式与互联网上的PC共享屏幕也欢迎。

0 个答案:

没有答案