Open jayadamsmorgan opened 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.
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!