An easy task----how to make a camera view to a Image?

Hi, everyone, I’m a new comer to JME, so I’m confusing about my task and I need some clue.



My task is described as follow.



First, I should place a fixed camera in the top side of a scene and the camera face to the origin point. I think I should use the setLocation and setDirection to complete that. However, I’m not sure about the vector. Is it setLocation(new Vector3f(0,0,50)) and setDirection(new Vector3f(0,0,-1))?



second step is a more complex one. As I said in the title, I should change the view which the camera have seen to the Image class. It’s sort of like the Render to Texture work, however I need Image instead of texture at last.



Maybe it’s an easy task, but I got no ideas about how to achieve my goal. So, could anyone help me please? Giving me some codes would be better.Thanks very much!!!

What kind of image? Like a java.awt.Image?



If so… look for ScreenshotAppState. There might be some clues in there.

pspeed said:
What kind of image? Like a java.awt.Image?

If so... look for ScreenshotAppState. There might be some clues in there.

Thanks for your replying.

Yes, I need a java.awt.Image as a result. In fact, I need this Image object to initiate an ImageIcon. Unfortunately, my jme has no ScreenshotAppState in com.jme.app, maybe it's an old version. However I cannot change it to a new one because the project is almost completed. You know, it's a big project and many people are working in there.

Actually, I have a little idea now. Let's see. I will use the similar skill of Render To Texture. I found a function----void render(Spatial spat, Texture tex) in TextureRenderer.java. Use this, I can get a texture(second parameter) from the specified camera I said below. And in the Texture.java there is a function named getImage().

However, there are some problems in my solution.
1. I don't how to initiate a camera correctly.
2.I'm not sure with the first parameter spatial in render(Spatial spat, Texture tex). There is a root node in my program, should I use this ?
3. the getImage() function would return a Image that is belong to JME. But I need one in AWT to be ImageIcon, how to change that?

would you please help me to finish that? I really appreciate if you give me more details. Thank you very much.

Jme3 beta definitely has ScreenshotAppState in com.jme3.app.state

It will automatically take a screenshot for you and save it to the directory you are running the app in. After the image has saved, you can read it in as an image icon.


1. I don't how to initiate a camera correctly.

With the camera set the position, and the look at point: camera.lookAt(position, upVector);
It works just like you think it would: look at this point.
Sploreg said:
Jme3 beta definitely has ScreenshotAppState in com.jme3.app.state
It will automatically take a screenshot for you and save it to the directory you are running the app in. After the image has saved, you can read it in as an image icon.

With the camera set the position, and the look at point: camera.lookAt(position, upVector);
It works just like you think it would: look at this point.

Thanks man. Unfortunately, I cannnot update to Jme3 because there are many people in this project.

Could you please offer another solution about this problem? Or point out errors of my solution that was said before. Thank you very much.

Are you using JME2? Or just not yet up to JME3 beta?



If you are at least up to JME3 then you can look at screen shot app state to see what it does. It’s been around a while now in JME3 terms.



Otherwise, I don’t know what to say as I have no experience with the old JME2.

pspeed said:
Are you using JME2? Or just not yet up to JME3 beta?

If you are at least up to JME3 then you can look at screen shot app state to see what it does. It's been around a while now in JME3 terms.

Otherwise, I don't know what to say as I have no experience with the old JME2.

Yeah, I still used the old JME. Anyway, thanks dude.

Maybe I should download the new JME to find some clue even though I cannot update my project.

hi all, i have the same problem, but don’t understand how to use ScreenshotAppState, anyone have an example? thank you very much and good year at everyone

[java]

// create the sate and attach it

ScreenshotAppState state = new ScreenshotAppState();

stateManager.attach(state);



// map a key to the state’s onAction() method

// …



// when the key is hit call this:

state.onAction(null,true,0);



// the picture will be saved to the directory where your app is running

[/java]

Perfect, thank you very much for your help

i’m sorry but i have a problem, i follow your instruction but i obtain two screenshot every time I press the key.

have you any idea?

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/random-sound/#post-156370

More specifically, remember to distinguish the down from the up.



More generally, if you put some printlns in your code or added a breakpoint there you might have seen exactly what was going on in less time than it took to post.

I did it but i have a problem ad i don’t know how rosolve it. this is my code:

[java]

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“ScreenShot”) && keyPressed) {

state.onAction(null,true,0);

getRGBColor(400,300);

}



public void getRGBColor(int sx, int sy){

File file = new File(“immagine.png”);

BufferedImage image;

try {

image = ImageIO.read(file);

int clr= image.getRGB(sx,sy);

int red = (clr & 0x00ff0000) >> 16;

int green = (clr & 0x0000ff00) >> 8;

int blue = clr & 0x000000ff;

System.out.println("Red Color value = "+ red);

System.out.println("Green Color value = "+ green);

System.out.println("Blue Color value = "+ blue);

} catch (IOException e) {



e.printStackTrace();

}



}

[/java]



my problem is that when method “getRGBColor” is called, the image has not yet saved, so it can’t open it and it throws the exception (javax.imageio.IIOException: Can’t read input file!)

I have tried to insert a delay, or created a new thread for that method, it but does not work.

File(“immagine.png”); → And the spelling and location of the file is correct??

yes it’s correct, the image is saved in the program folder, and if i run the application an other time, it reads the previuos image and it gets the color correctly

Do a cleanup.

I tried it, both before and after but the problem remains

[java]

if (name.equals(“ScreenShot”) && keyPressed) {

state.cleanup();

state.onAction(null,true,0);



getRGBColor(400,300);

}

[/java]



I think the problem is that the image is saved only after the call of the method getRBGColor, so it can’t open it because it has not yet been saved.

In fact, if i stop the application, then the image is saved.

Is there a way to execute the method getRBGColor only after the image is saved?

I tried to put a delay or to execute the method in a new thread but nothing

About the cleanup I meant a clean / build in jMP xD. But it’s not the case. And about the image, does it already exist? Or do you wanna create it in runtime?

when i press a key i want to create the image and open it to obtain pixel color, it’s possible to do it in runtime?