简体   繁体   English

JavaCV-提高网络摄像头质量

[英]JavaCV - Improve Webcam Quality

im currently trying to implement a webcam scanner. 我目前正在尝试实施网络摄像头扫描仪。 My current problem is that i can't capture a high quality image when using Java. 我当前的问题是使用Java时无法捕获高质量的图像。 The camera i use has 8MP and produces great images when i use the Windows "Camera"-App. 当我使用Windows“ Camera” -App时,我使用的相机具有8MP并产生出色的图像。 Sadly i can't replicate this quality with JavaCV. 可悲的是,我无法用JavaCV复制这种质量。

This is the (important) code i'm using to capture an Image: 这是我用来捕获图像的(重要)代码:

FrameGrabber grabber;
private Java2DFrameConverter frameConverter;
public JavaCVCamera(String imagePath, double rotation) {
    this.frameConverter = new Java2DFrameConverter();
    this.imagePath = imagePath;
    this.rotation = rotation;
    this.grabber = new OpenCVFrameGrabber(0);
    this.grabber.setImageWidth(3264);
    this.grabber.setImageHeight(2448);
    try {
        this.grabber.start();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void saveImageToFile(boolean applyUpscaling, boolean applyRotation) {
    File folder = new File(imagePath);
    if (!folder.exists()) {
        folder.mkdirs();
    }

    try {
        BufferedImage img = frameConverter.convert(grabber.grab());

        if (applyUpscaling) {
            img = Thumbnails.of(img).forceSize(3264, 2448).asBufferedImage();
        }

        if (applyRotation) {
            img = rotateImageByDegrees(img, rotation);
        }

        File path = new File(imagePath + "/img_"
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss")) + "." + "png");
        ImageIO.write(img, "png", path);
        LOGGER.info("Captured Image: {}", path.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

(Note: i dont use upscaling to make the images larger) (注意:我不使用放大来放大图像)

For your understanding, here's an example: 为了您的理解,下面是一个示例:

Is there some way to capture images with the same resolution and quality as the Windows Camera App? 有什么方法可以捕获分辨率和质量与Windows Camera App相同的图像?

Kind Regards, QUE 亲切的问候,QUE

A BufferedImage is very memory consuming in Java as it stores the image in a Bitmap like image format. BufferedImage在Java中非常消耗内存,因为它以类似于图像格式的位图存储图像。 Other image formats that use compression consume less memory. 使用压缩的其他图像格式消耗更少的内存。 But how to use them with JavaCV? 但是如何在JavaCV中使用它们?

First of all, check with a profiler app which code is responsible for the extensive memory consumption of your implementation. 首先,使用探查器应用程序检查哪个代码负责实现中的大量内存消耗。 Hopefully the Frame stores the image data still in compressed form; 希望框架以压缩形式存储图像数据; and when executing the frameConverter.convert it consumes the high ammount of memory. 并且在执行frameConverter.convert时会消耗大量内存。 In this case, search for a solution that stores the image as a JPEG (or any other compressed data) in a byte[] - for displaying the image, you can draw the image directly eg to a Graphics2D element w/o converting it to a BufferedImage first. 在这种情况下,搜索将图像以JPEG(或任何其他压缩数据)的形式存储在byte []中的解决方案-为了显示图像,您可以直接将图像绘制到例如Graphics2D元素上,而无需将其转换为首先是BufferedImage。 Use ToolkitImage therefore, which is able to draw a JPEG image from an InputStream image source. 因此,请使用ToolkitImage,它可以从InputStream图像源绘制JPEG图像。 This can be a ByteArrayInputStream, if you were able to store your high-res image in a JPEG directly from the JavaCV. 如果您能够直接从JavaCV将高分辨率图像存储为JPEG,则可以为ByteArrayInputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM