sarxos / webcam-capture

The goal of this project is to allow integrated or USB-connected webcams to be accessed directly from Java. Using provided libraries users are able to read camera images and detect motion. Main project consist of several sub projects - the root one, which contains required classes, build-in webcam driver compatible with Windows, Linux and Mac OS, which can stream images as fast as your camera can serve them (up to 50 FPS). Main project can be used standalone, but user is able to replace build-in driver with different one - such as OpenIMAJ, GStreamer, V4L4j, JMF, LTI-CIVIL, FMJ, etc.
http://webcam-capture.sarxos.pl
MIT License
2.27k stars 1.11k forks source link

Issue with creating libgdx pixmap from imagebytes #502

Open jayadamsmorgan opened 8 years ago

jayadamsmorgan commented 8 years ago

Hi! I have a libgdx project and I need to convert BufferedImage to Libgdx Texture or ByteBuffer to LibgdxTexture. I heard that BufferedImage is incompatible with libgdx, so decided to create pixmap from bytes from function getImageBytes. I tried a lot of ways, but nothing is working. Help me, please!

Crigges commented 8 years ago

I was trying something similar, but working with BufferedImages is pretty slow you, so realy should use the getImageBytes() funtion. From there on you can take a look at this code here, just to get an idea how to draw the bytebuffer contents onto a texture:

package systems.crigges.gui;

import java.awt.Dimension;
import java.nio.ByteBuffer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.maps.tiled.renderers.IsometricStaggeredTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.github.sarxos.webcam.Webcam;

public class CameraActor extends Actor {
    private CameraController controller;
    private Texture img;
    private Dimension recordingSize;

    public void setController(CameraController c) {
        this.controller = c;
        if(c != null){
            recordingSize = c.getViewSize();
            img = new Texture(recordingSize.width, recordingSize.height, Format.RGB888);
        }
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        if(controller == null){
            return;
        }
        ByteBuffer imageByteBuffer = controller.getBuffer();
        if(imageByteBuffer == null){
            return;
        }
        if(!controller.getViewSize().equals(recordingSize)){
            recordingSize = controller.getViewSize();
            img.dispose();
            img = new Texture(recordingSize.width, recordingSize.height, Format.RGB888);
        }
        //check for old buffer
        if(imageByteBuffer.capacity() != recordingSize.width * recordingSize.height * 3){
            System.out.println("invaild buffer");
            return;
        }
        Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0);
        img.bind();
        imageByteBuffer.position(0);
        Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGB, recordingSize.width, recordingSize.height, 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, imageByteBuffer);
        Gdx.gl.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_LINEAR);
        Gdx.gl.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MAG_FILTER, GL20.GL_LINEAR);
        Gdx.gl.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_S, GL20.GL_CLAMP_TO_EDGE);
        Gdx.gl.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_WRAP_T, GL20.GL_CLAMP_TO_EDGE);
        batch.draw(img, getX(), getY());
    }

}

For the full code i added you to the yet private repo.