Java 从ip摄像机捕获视频
我正在尝试将视频从ip摄像头捕获到我的应用程序中,这是一个例外Java 从ip摄像机捕获视频,java,video-streaming,ip-camera,Java,Video Streaming,Ip Camera,我正在尝试将视频从ip摄像头捕获到我的应用程序中,这是一个例外 com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0x0d 0x0a at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method) at sun.awt.image.codec.JPEGImageDecoderImpl.decode
com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0x0d 0x0a
at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method)
at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(Unknown Source)
at test.AxisCamera1.readJPG(AxisCamera1.java:130)
at test.AxisCamera1.readMJPGStream(AxisCamera1.java:121)
at test.AxisCamera1.readStream(AxisCamera1.java:100)
at test.AxisCamera1.run(AxisCamera1.java:171)
at java.lang.Thread.run(Unknown Source)
它在image=decoder.decodeAsBufferedImage()处出现异常
这是我正在尝试的代码
private static final long serialVersionUID = 1L;
public boolean useMJPGStream = true;
public String jpgURL = "http://ip here/video.cgi/jpg/image.cgi?resolution=640×480";
public String mjpgURL = "http://ip here /video.cgi/mjpg/video.cgi?resolution=640×480";
DataInputStream dis;
private BufferedImage image = null;
public Dimension imageSize = null;
public boolean connected = false;
private boolean initCompleted = false;
HttpURLConnection huc = null;
Component parent;
/** Creates a new instance of AxisCamera */
public AxisCamera1(Component parent_) {
parent = parent_;
}
public void connect() {
try {
URL u = new URL(useMJPGStream ? mjpgURL : jpgURL);
huc = (HttpURLConnection) u.openConnection();
// System.out.println(huc.getContentType());
InputStream is = huc.getInputStream();
connected = true;
BufferedInputStream bis = new BufferedInputStream(is);
dis = new DataInputStream(bis);
if (!initCompleted)
initDisplay();
} catch (IOException e) { // incase no connection exists wait and try
// again, instead of printing the error
try {
huc.disconnect();
Thread.sleep(60);
} catch (InterruptedException ie) {
huc.disconnect();
connect();
}
connect();
} catch (Exception e) {
;
}
}
public void initDisplay() { // setup the display
if (useMJPGStream)
readMJPGStream();
else {
readJPG();
disconnect();
}
imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
setPreferredSize(imageSize);
parent.setSize(imageSize);
parent.validate();
initCompleted = true;
}
public void disconnect() {
try {
if (connected) {
dis.close();
connected = false;
}
} catch (Exception e) {
;
}
}
public void paint(Graphics g) { // used to set the image on the panel
if (image != null)
g.drawImage(image, 0, 0, this);
}
public void readStream() { // the basic method to continuously read the
// stream
try {
if (useMJPGStream) {
while (true) {
readMJPGStream();
parent.repaint();
}
} else {
while (true) {
connect();
readJPG();
parent.repaint();
disconnect();
}
}
} catch (Exception e) {
;
}
}
public void readMJPGStream() { // preprocess the mjpg stream to remove the
// mjpg encapsulation
readLine(3, dis); // discard the first 3 lines
readJPG();
readLine(2, dis); // discard the last two lines
}
public void readJPG() { // read the embedded jpeg image
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
image = decoder.decodeAsBufferedImage();
} catch (Exception e) {
e.printStackTrace();
disconnect();
}
}
public void readLine(int n, DataInputStream dis) { // used to strip out the
// header lines
for (int i = 0; i < n; i++) {
readLine(dis);
}
}
public void readLine(DataInputStream dis) {
try {
boolean end = false;
String lineEnd = "\n"; // assumes that the end of the line is marked
// with this
byte[] lineEndBytes = lineEnd.getBytes();
byte[] byteBuf = new byte[lineEndBytes.length];
while (!end) {
dis.read(byteBuf, 0, lineEndBytes.length);
String t = new String(byteBuf);
System.out.print(t); // uncomment if you want to see what the
// lines actually look like
if (t.equals(lineEnd))
end = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("in Run...................");
connect();
readStream();
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AxisCamera1 axPanel = new AxisCamera1(jframe);
new Thread(axPanel).start();
jframe.getContentPane().add(axPanel);
jframe.pack();
jframe.show();
}
}
private static final long serialVersionUID=1L;
公共布尔值useMJPGStream=true;
公共字符串jpgURL=”http://ip 此处/video.cgi/jpg/image.cgi?分辨率=640×480”;
公共字符串mjpgURL=”http://ip 此处/video.cgi/mjpg/video.cgi?分辨率=640×480”;
数据输入流dis;
私有缓冲区映像=空;
公共维度imageSize=null;
公共布尔连接=false;
私有布尔initCompleted=false;
HttpURLConnection huc=null;
成分父代;
/**创建AxisCamera的新实例*/
公共AxisCamera1(组件父级){
家长=家长;
}
公共void connect(){
试一试{
URL u=新URL(使用MJPGStream?mjpgURL:jpgURL);
huc=(HttpURLConnection)u.openConnection();
//System.out.println(huc.getContentType());
InputStream=huc.getInputStream();
连接=真;
BufferedInputStream bis=新的BufferedInputStream(is);
dis=新数据输入流(bis);
如果(!initCompleted)
initDisplay();
}捕获(IOE异常){//如果不存在连接,请等待并重试
//同样,不是打印错误
试一试{
huc.disconnect();
睡眠(60);
}捕获(中断异常ie){
huc.disconnect();
connect();
}
connect();
}捕获(例外e){
;
}
}
public void initDisplay(){//设置显示
如果(使用mjpgstream)
readMJPGStream();
否则{
readJPG();
断开连接();
}
imageSize=新维度(image.getWidth(this)、image.getHeight(this));
setPreferredSize(图像大小);
父.setSize(imageSize);
parent.validate();
initCompleted=true;
}
公共空间断开连接(){
试一试{
如果(已连接){
dis.close();
连接=错误;
}
}捕获(例外e){
;
}
}
public void paint(图形g){//用于设置面板上的图像
如果(图像!=null)
g、 drawImage(图像,0,0,this);
}
public void readStream(){//连续读取
//溪流
试一试{
如果(使用mjpgstream){
while(true){
readMJPGStream();
parent.repaint();
}
}否则{
while(true){
connect();
readJPG();
parent.repaint();
断开连接();
}
}
}捕获(例外e){
;
}
}
public void readMJPGStream(){//预处理mjpg流以删除
//mjpg封装
readLine(3,dis);//放弃前3行
readJPG();
readLine(2,dis);//放弃最后两行
}
public void readJPG(){//读取嵌入的jpeg图像
试一试{
JPEGImageDecoder解码器=JPEGCodec.createJPEGDecoder(dis);
image=decoder.decodeAsBufferedImage();
}捕获(例外e){
e、 printStackTrace();
断开连接();
}
}
public void readLine(intn,DataInputStream dis){//用于除去
//标题行
对于(int i=0;i
任何关于我在这里做错了什么的建议???jpeg魔术代码是“0xff 0xd8”而不是“0x0d 0x0a”(请参见[1]),这就是代码失败的原因。也许您在readMJPGStream()中删除行的代码部分太饿了:)
[1] 在这个代码片段中,您尝试读取一个mjpg流,我相信您必须做得比删除前两行更好。也许这会有帮助:谢谢你的帮助..问题是读mjpg的行…最后我让它工作了..)