起初,我仅使用Java来恢复机器人的图像并将其显示在JFrame中,但此后我想将此处理编码为服务wb rest,提供机器人捕获的图像,然后将其显示在调用服务后的htmp页面,因此捕获的图像格式为java.awt.Image,但是在进行部署和服务测试后,出现错误500,因此Web服务返回的正确格式是什么? html页面
首先,我们需要实例化机器人类的对象,并创建一个侦听器类,该类实现一种以周期性方式捕获机器人图像的方法,并将其保存在全局变量中,在与机器人连接的服务中,将收听者与之关联。
@Stateless
@Path("emp.com.classes.rbt")
public class Robotino extends emp.com.rest.AbstractFacade<emp.com.classes.rbt> {
protected static final float speed = 0.2f;
protected static final float rotSpeed = 2f;
protected static final Robot robotino = new Robot();
Timer _timer;
private float vx=0.2f;
private float vy=0.0f;
private float omega=0.0f;
int i=0;
public volatile Image cameraImg;
public BufferedImage retImg;
public Robotino() {
super(emp.com.classes.rbt.class);
}
class OnTimeOut extends TimerTask
{
public void run()
{
robotino.setVelocity( vx, vy, omega );
i++;
}
}
private class RobotListenerImpl implements RobotListener
{
@Override
public void onImageReceived(Image img)
{
cameraImg = img;
}
@Override
public void onConnected()
{
}
@Override
public void onDisconnected()
{
}
@Override
public void onError(String error)
{
}
@Override
public void onOdometryReceived(double x, double y, double phi)
{
}
}
/**
* Retrieves representation of an instance of emp.rest.Zplug
* @return an instance of java.lang.String
* @throws java.io.IOException
*/
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_HTML})
@Path("camera")
public Image camera() throws IOException {
robotino.connect("127.0.0.1:12080", false);
robotino.addListener( new RobotListenerImpl() );
retImg=(BufferedImage) cameraImg;
return retImg;
}
@Override
protected EntityManager getEntityManager() {
throw new UnsupportedOperationException("Not supported yet.");
}
}