乐趣区

关于java:Java截图优化

应为要用 java 截图,而后传递给 opencv 解决,java 中截图是应用下边的代码:

        Robot robot = new Robot();
        BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(0, 0, 1920, 1080));

然而在我的电脑的全屏截图要 50ms 左右,但当我缩小截图区域后,耗时会有一个显著的成比例缩小,这让我想要看一下为什么有这样。

private synchronized BufferedImage[]
            createCompatibleImage(Rectangle screenRect, boolean isHiDPI) {checkScreenCaptureAllowed();

        checkValidRect(screenRect);

        BufferedImage lowResolutionImage;
        BufferedImage highResolutionImage;
        DataBufferInt buffer;
        WritableRaster raster;
        BufferedImage[] imageArray;
        // 因为调用截图 api 返回的像素为 int[],所有这里确定 RGB 的值别离在那些位
        if (screenCapCM == null) {
            /*
             * Fix for 4285201
             * Create a DirectColorModel equivalent to the default RGB ColorModel,
             * except with no Alpha component.
             */

            screenCapCM = new DirectColorModel(24,
                    /* red mask */ 0x00FF0000,
                    /* green mask */ 0x0000FF00,
                    /* blue mask */ 0x000000FF);
        }

        int[] bandmasks = new int[3];
        bandmasks[0] = screenCapCM.getRedMask();
        bandmasks[1] = screenCapCM.getGreenMask();
        bandmasks[2] = screenCapCM.getBlueMask();

        // 感觉是避免反复截图,期待下一帧吧
        Toolkit.getDefaultToolkit().sync();

        //
        GraphicsConfiguration gc = GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().
                getDefaultConfiguration();
        gc = SunGraphicsEnvironment.getGraphicsConfigurationAtPoint(gc, screenRect.getCenterX(), screenRect.getCenterY());

        AffineTransform tx = gc.getDefaultTransform();
        double uiScaleX = tx.getScaleX();
        double uiScaleY = tx.getScaleY();
        int[] pixels;
        // 我电脑这里 uiScaleX  和 uiScaleY 都是 1.x,所以走的 else 分支
        if (uiScaleX == 1 && uiScaleY == 1) {pixels = peer.getRGBPixels(screenRect);
            buffer = new DataBufferInt(pixels, pixels.length);

            bandmasks[0] = screenCapCM.getRedMask();
            bandmasks[1] = screenCapCM.getGreenMask();
            bandmasks[2] = screenCapCM.getBlueMask();

            raster = Raster.createPackedRaster(buffer, screenRect.width,
                    screenRect.height, screenRect.width, bandmasks, null);
            SunWritableRaster.makeTrackable(buffer);

            highResolutionImage = new BufferedImage(screenCapCM, raster,
                    false, null);
            imageArray = new BufferedImage[1];
            imageArray[0] = highResolutionImage;

        } else {
            Rectangle scaledRect;
            if (peer.useAbsoluteCoordinates()) {
                scaledRect = toDeviceSpaceAbs(gc, screenRect.x,
                        screenRect.y, screenRect.width, screenRect.height);
            } else {
                scaledRect = toDeviceSpace(gc, screenRect.x,
                        screenRect.y, screenRect.width, screenRect.height);
            }
            // 这里调用本地办法截图,这里是一个耗时点
            pixels = peer.getRGBPixels(scaledRect);
            // 构建解析 pixels 中数据的对象,buffer = new DataBufferInt(pixels, pixels.length);
            raster = Raster.createPackedRaster(buffer, scaledRect.width,
                    scaledRect.height, scaledRect.width, bandmasks, null);
            SunWritableRaster.makeTrackable(buffer);

            highResolutionImage = new BufferedImage(screenCapCM, raster,
                    false, null);


            // 这里大略意思就是,依据高分辨率图像,生成低分辨率图像,然而 drawImage 办法也挺耗时的
            lowResolutionImage = new BufferedImage(screenRect.width,
                    screenRect.height, highResolutionImage.getType());
            Graphics2D g = lowResolutionImage.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.setRenderingHint(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawImage(highResolutionImage, 0, 0,
                    screenRect.width, screenRect.height,
                    0, 0, scaledRect.width, scaledRect.height, null);
            g.dispose();

            if(!isHiDPI) {imageArray = new BufferedImage[1];
                imageArray[0] = lowResolutionImage;
            } else {imageArray = new BufferedImage[2];
                imageArray[0] = lowResolutionImage;
                imageArray[1] = highResolutionImage;
            }

        }

        return imageArray;
    }

我把 pixels = peer.getRGBPixels(scaledRect); 办法独自拿进去测试,速度是 26ms,也就是其余操作是优化的。
我最开始是查看 opencv 中是否有用一个 int 报错一个像素的 rgb 值的内容,然而如同并没有。所以我就尝试手动解析 pixels,从中提取 r,g,b 的值。

        // 截图
        pixels = peer.getRGBPixels(screenRect);
        // 解析像素
        int length = screenRect.width * screenRect.height;
        byte[] imgBytes = new byte[length * 3];
        int byteIndex = 0;
        for (int i = 0, pixel = 0; i < length; i++) {pixel = pixels[i];
            //  pixel 中是依照 rgb 格局排序,然而 opencv 默认是 bgr 格局
            imgBytes[byteIndex++] = (byte) (pixel);
            pixel = pixel >> 8;
            imgBytes[byteIndex++] = (byte) (pixel);
            imgBytes[byteIndex++] = (byte) (pixel >> 8);
        }

通过测试,这里解析只须要 3~4ms,而后把这个 byte[] 传递给 mat 就好了。整体截一张图并传递给 opencv 的耗时为 30ms。

最初试了下在屏幕画面继续变动的状况下,一次截图并传给 opencv 的耗时为 35ms。

退出移动版