I am having an issue when attempting to create and use a texture based on a .jpg image I have taken from the camera on Android. I can successfully capture the picture (verified by writing the file to SD card and examining the resulting jpg). I can then also load the texture from the file by using the asset manager and have it display correctly on a material.
The problem is that I do not really want to write the file to disk but load it directly from the byte[] captured from the camera. When I try this, the resulting texture is just a series of lines and pixels, much like the static you would see on a TV screen. The important code is as follows:
Camera capture/convert code
[java]
final ImageAppState imageState = app.getStateManager().getState(ImageAppState.class);
FileOutputStream outStream = null;
try {
// write to local sandbox file system
// outStream =
// CameraDemo.this.openFileOutput(String.format("%d.jpg",
// System.currentTimeMillis()), 0);
// Or write to sdcard
ImageAppState.imgFile = String.format(
"/sdcard/%d.jpg", System.currentTimeMillis());
outStream = new FileOutputStream(ImageAppState.imgFile);
outStream.write(bytes);
outStream.close();
Log.e("error", "onPictureTaken - wrote bytes: " + bytes.length);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
final Parameters params = camera.getParameters();
final Image img= new Image(Format.RGBA8,
params.getPictureSize().width, params.getPictureSize().height, BufferUtils.createByteBuffer(bytes));
imageState.setImage(img);
[/java]
Update code (in another class)
[java]
@Override
public void simpleRender(RenderManager rm) {
ImageAppState imageState = getStateManager().getState(ImageAppState.class);
Image image = imageState.getImage();
if (image != null) {
tex_ml = new Texture2D(image);
// Material already set on Geometry
mat.setTexture("ColorMap", tex_ml);
imageState.setImage(null);
}
}
[/java]
Can anyone shed any light on why the texture cannot be properly displayed? I would also like to point out that sometimes the application crashes in native code when attempting to set the texture sometimes, depending on the size I set on the image it seems.
Thanks very much normen, lovely stuff
For anyone else who wants to do what I described above, the pertinent pieces of code from the referenced zip file are as follows:
[java]
private class ByteArrayInfo extends com.jme3.asset.AssetInfo {
private byte[] data;
/**
-
@param manager
-
@param key
/
public ByteArrayInfo(byte[] data) {
super(null, new com.jme3.asset.TextureKey("ByteArray", true));
this.data = data;
}
/ (non-Javadoc)
-
@see com.jme3.asset.AssetInfo#openStream()
*/
@Override
public InputStream openStream() {
return new ByteArrayInputStream(data);
}
}
[/java]
Usage:
[java]
AndroidImageLoader loader = new AndroidImageLoader();
Image img = (com.jme3.texture.Image) ail.load(new ByteArrayInfo(bytes));
[/java]
Incidentally, when I attempted to run the full application contained within the zip, I only got blank white faces on the shape rendered on screen which is where I assume the camera ‘preview’ image was supposed to be displayed. Not really sure why but could be a multitude of reasons…
1 Like