MJPEG on Android anyone?

All your problems with Audio, Video and Images.

Re: MJPEG on Android anyone?

Postby narenayak » Wed Oct 19, 2011 11:24 am

can anyone share the complete android package
narenayak
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 19, 2011 11:18 am

Top

Re: MJPEG on Android anyone?

Postby coersum » Tue Nov 08, 2011 3:56 pm

I would really like to see an eclipse project including the xml and code to integrate the viewer into an app... I am having lots of trouble getting it into an app, the project that was posted in the early posts worked but it doesn't show how to integrate it into an application.

Thank you.
coersum
Freshman
Freshman
 
Posts: 2
Joined: Tue Nov 08, 2011 3:49 pm

Re: MJPEG on Android anyone?

Postby silasisland » Sun Nov 27, 2011 9:48 pm

I'm looking for a way to integrate the viewer to an HTML file, how can I go upon doing this?
silasisland
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Nov 27, 2011 9:45 pm

Re: MJPEG on Android anyone?

Postby shenwu35 » Mon Dec 26, 2011 2:58 am

HI All,
I have used the demo to see the mjpeg stream on my app in android, But i have encountered a new problem:

i want to save the mjpeg stream to the sdcard .however, the mjpeg video file was not supported in android. so how could encode mjpeg video into 3gp or mp4 format and then store them on sdcard ,at last ,i can play back the 3gp or mp4 video on my android phone ,thanks in advance.
shenwu35
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Dec 26, 2011 2:35 am

Re: MJPEG on Android anyone?

Postby MJ2011 » Tue Dec 27, 2011 2:14 pm

Hi all,
I’m new on android development and I’m confronting a problem regarding remote my IPcam
For my application I’m using a Foscam FI8918W IPcam.
I have the video Stream is working perfectly on Android 2.2, I have to integrate now the remote option to move (up,down,left,right,screen capture)
Refer to the IPCam SDK e.g move up need just to send the follow request to the server
http://ipcam.dyndns.org:80/decoder_cont ... pwd=XXX123
I have used a HttpGet but I’m prompted to enter login and password, is there any possibly better implementation for this option
Any hint will be appreciated .Thanks in advance
MJ2011
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Dec 26, 2011 2:26 pm

Re: MJPEG on Android anyone?

Postby SkolVikingsGuy » Sat Feb 11, 2012 8:21 am

I'm using this code and it's very helpful, thanks! I have one question.

Why in stopPlayback() is the thread.join() call required? It seems like setting mRun to false is enough to get the thread to quit when it gets the chance. With the thread.join() the main thread needs to wait for the thread to complete and it might be hung up for some reason, long enough at least to cause an ANR error.

Thanks for the help.

public void stopPlayback() {
mRun = false;
boolean retry = true;
while(retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
SkolVikingsGuy
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 09, 2012 6:18 pm

Top

Re: MJPEG on Android anyone?

Postby qdim » Tue Feb 28, 2012 9:59 am

hi all
good work source.
this source code used in project http://code.google.com/p/android-camera-axis/
for this code change
public CameraControl(Camera cam, Activity activity) {
this.cam = cam;
Application a;
this.activity = activity;
this.initConfig();
// this.loadConfig(); // <--------this
}
for original code
in source change
String URL = "http://192.168.0.171:8080/?action=stream";


mjpg-streamer work fine
qdim
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 28, 2012 9:48 am

Re: MJPEG on Android anyone?

Postby SkolVikingsGuy » Tue Mar 20, 2012 4:25 am

Please help! I'm just about to release a product with the very helpful code posted here. But I notice that a fix to Ice Cream Sandwhich slows the code way down. The fix is in the SocketInputBuffer() constructor, which takes a buffersize value. The old code honored that value, and in my case it's 87K. The fix is to ignore the value and force it to be 8K. The description for the fix is here:

https://www.gitorious.org/rowboat/exter ... 7b8da1a07a

I'm not exactly sure why that slows things down, but it does. Anybody have any ideas on how to work around this issue?
SkolVikingsGuy
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 09, 2012 6:18 pm

Re: MJPEG on Android anyone?

Postby joaodotneto » Wed Mar 21, 2012 3:18 am

I have the same Issue, when it calls getEndOfSeqeunce in MjpegInputStream i have an IOException.
joaodotneto
Freshman
Freshman
 
Posts: 2
Joined: Fri Mar 09, 2012 2:52 am

Re: MJPEG on Android anyone?

Postby wrochadel » Mon Apr 09, 2012 2:52 am

People,
Cool! And is what I need, but could not enter the Android.
Does anyone have a project?

Thank you!
wrochadel
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 08, 2012 10:05 pm

Re: MJPEG on Android anyone?

Postby proyectohector » Tue Jul 10, 2012 11:54 am

Hello

I am making an android application where I need to play video from a camera IP, specifically my ip is:
"http://192.168.0.20/MJPEG.CGI"

I am using the following code to display the video:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.camWifibot;
  2.  
  3. import java.io.InputStream;
  4. import java.net.HttpURLConnection;
  5.  
  6. import com.video.MjpegInputStream;
  7. import com.video.MjpegView;
  8. import android.app.Activity;
  9. import android.graphics.Bitmap;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.widget.ImageView;
  14.  
  15. public class CamWifibot3Activity extends Activity {
  16.     private MjpegView mv;
  17.     HttpURLConnection videoCon;
  18.     InputStream stream;
  19.    
  20.     ImageView camCapture;
  21.     Thread threadImagenes;
  22.     public static boolean fin = false;
  23.     MjpegInputStream mIn = null;
  24.    
  25.    
  26.    
  27.     /** Called when the activity is first created. */
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.main);
  32.         String URL = "192.168.0.20/MJPEG.CGI";
  33.        
  34.         camCapture = (ImageView) findViewById(R.id.camImage);
  35.         mIn = MjpegInputStream.read(URL);
  36.        
  37.        
  38.         final Handler myHandler = new Handler(){
  39.            
  40.             public void handleMessage(Message msg) {
  41.                 camCapture.setImageBitmap((Bitmap)msg.obj);
  42.             }
  43.  
  44.         };
  45.            
  46.         threadImagenes = new Thread() {
  47.             @Override
  48.             public void run() {
  49.                 try {
  50.                     while (!fin) {
  51.                         System.out.println("Empezamos con las imagenes");
  52.                         Message msg = myHandler.obtainMessage();
  53.                         msg.obj = mIn.readMjpegFrame();
  54.                         myHandler.sendMessage(msg);
  55.                     }
  56.  
  57.                 } catch (Exception e) {
  58.                     e.printStackTrace();
  59.                 }
  60.             }
  61.         };
  62.         threadImagenes.start();
  63.        
  64.        
  65.  
  66.     }
  67. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.video;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.DataInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URI;
  9. import java.util.Properties;
  10.  
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15.  
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18.  
  19. public class MjpegInputStream extends DataInputStream {
  20.     private final byte[] SOI_MARKER = { (byte) 0xFF, (byte) 0xD8 };
  21.     private final byte[] EOF_MARKER = { (byte) 0xFF, (byte) 0xD9 };
  22.     private final String CONTENT_LENGTH = "Content-Length";
  23.     private final static int HEADER_MAX_LENGTH = 100;
  24.     private final static int FRAME_MAX_LENGTH = 40000 + HEADER_MAX_LENGTH;
  25.     private int mContentLength = -1;
  26.    
  27.     public static MjpegInputStream read(String url) {
  28.         HttpResponse res;
  29.         DefaultHttpClient httpclient = new DefaultHttpClient();        
  30.         try {
  31.             res = httpclient.execute(new HttpGet(URI.create(url)));
  32.             return new MjpegInputStream(res.getEntity().getContent());                
  33.         } catch (ClientProtocolException e) {
  34.         } catch (IOException e) {}
  35.         return null;
  36.     }
  37.    
  38.     public MjpegInputStream(InputStream in) { super(new BufferedInputStream(in, FRAME_MAX_LENGTH)); }
  39.    
  40.     private int getEndOfSeqeunce(DataInputStream in, byte[] sequence) throws IOException {
  41.         int seqIndex = 0;
  42.         byte c;
  43.         for(int i=0; i < FRAME_MAX_LENGTH; i++) {
  44.             c = (byte) in.readUnsignedByte();
  45.             if(c == sequence[seqIndex]) {
  46.                 seqIndex++;
  47.                 if(seqIndex == sequence.length) return i + 1;
  48.             } else seqIndex = 0;
  49.         }
  50.         return -1;
  51.     }
  52.    
  53.     private int getStartOfSequence(DataInputStream in, byte[] sequence) throws IOException {
  54.         int end = getEndOfSeqeunce(in, sequence);
  55.         return (end < 0) ? (-1) : (end - sequence.length);
  56.     }
  57.  
  58.     private int parseContentLength(byte[] headerBytes) throws IOException, NumberFormatException {
  59.         ByteArrayInputStream headerIn = new ByteArrayInputStream(headerBytes);
  60.         Properties props = new Properties();
  61.         props.load(headerIn);
  62.         return Integer.parseInt(props.getProperty(CONTENT_LENGTH));
  63.     }    
  64.  
  65.     public Bitmap readMjpegFrame() throws IOException {
  66.         mark(FRAME_MAX_LENGTH);
  67.         int headerLen = getStartOfSequence(this, SOI_MARKER);
  68.         reset();
  69.         byte[] header = new byte[headerLen];
  70.         readFully(header);
  71.         try {
  72.             mContentLength = parseContentLength(header);
  73.         } catch (NumberFormatException nfe) {
  74.             mContentLength = getEndOfSeqeunce(this, EOF_MARKER);
  75.         }
  76.         reset();
  77.         byte[] frameData = new byte[mContentLength];
  78.         skipBytes(headerLen);
  79.         readFully(frameData);
  80.         return BitmapFactory.decodeStream(new ByteArrayInputStream(frameData));
  81.     }
  82. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.video;
  2.  
  3. import java.io.IOException;
  4.  
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.Canvas;
  8. import android.graphics.Color;
  9. import android.graphics.Paint;
  10. import android.graphics.PorterDuff;
  11. import android.graphics.PorterDuffXfermode;
  12. import android.graphics.Rect;
  13. import android.graphics.Typeface;
  14. import android.util.AttributeSet;
  15. import android.util.Log;
  16. import android.view.SurfaceHolder;
  17. import android.view.SurfaceView;
  18.  
  19. public class MjpegView extends SurfaceView implements SurfaceHolder.Callback {
  20.     public final static int POSITION_UPPER_LEFT = 9;
  21.     public final static int POSITION_UPPER_RIGHT = 3;
  22.     public final static int POSITION_LOWER_LEFT = 12;
  23.     public final static int POSITION_LOWER_RIGHT = 6;
  24.  
  25.     public final static int SIZE_STANDARD = 1;
  26.     public final static int SIZE_BEST_FIT = 4;
  27.     public final static int SIZE_FULLSCREEN = 8;
  28.  
  29.     public static MjpegViewThread thread;
  30.     private MjpegInputStream mIn = null;
  31.     private boolean showFps = false;
  32.     private boolean mRun = false;
  33.     private boolean surfaceDone = false;
  34.     private Paint overlayPaint;
  35.     private int overlayTextColor;
  36.     private int overlayBackgroundColor;
  37.     private int ovlPos;
  38.     private int dispWidth;
  39.     private int dispHeight;
  40.     private int displayMode;
  41.     private boolean resume = false;
  42.  
  43.     private Context context;
  44.     private long delay;
  45.  
  46.     public class MjpegViewThread extends Thread {
  47.         private SurfaceHolder mSurfaceHolder;
  48.         private int frameCounter = 0;
  49.         private long start;
  50.         private Bitmap ovl;
  51.  
  52.         public MjpegViewThread(SurfaceHolder surfaceHolder, Context context) {
  53.             mSurfaceHolder = surfaceHolder;
  54.         }
  55.  
  56.         private Rect destRect(int bmw, int bmh) {
  57.             int tempx;
  58.             int tempy;
  59.             if (displayMode == MjpegView.SIZE_STANDARD) {
  60.                 tempx = (dispWidth / 2) - (bmw / 2);
  61.                 tempy = (dispHeight / 2) - (bmh / 2);
  62.                 return new Rect(tempx, tempy, bmw + tempx, bmh + tempy);
  63.             }
  64.             if (displayMode == MjpegView.SIZE_BEST_FIT) {
  65.                 float bmasp = (float) bmw / (float) bmh;
  66.                 bmw = dispWidth;
  67.                 bmh = (int) (dispWidth / bmasp);
  68.                 if (bmh > dispHeight) {
  69.                     bmh = dispHeight;
  70.                     bmw = (int) (dispHeight * bmasp);
  71.                 }
  72.                 tempx = (dispWidth / 2) - (bmw / 2);
  73.                 tempy = (dispHeight / 2) - (bmh / 2);
  74.                 return new Rect(tempx, tempy, bmw + tempx, bmh + tempy);
  75.             }
  76.             if (displayMode == MjpegView.SIZE_FULLSCREEN)
  77.                 return new Rect(0, 0, dispWidth, dispHeight);
  78.             return null;
  79.         }
  80.  
  81.         public void setSurfaceSize(int width, int height) {
  82.             synchronized (mSurfaceHolder) {
  83.                 dispWidth = width;
  84.                 dispHeight = height;
  85.             }
  86.         }
  87.  
  88.         private Bitmap makeFpsOverlay(Paint p, String text) {
  89.             Rect b = new Rect();
  90.             p.getTextBounds(text, 0, text.length(), b);
  91.             int bwidth = b.width() + 2;
  92.             int bheight = b.height() + 2;
  93.             Bitmap bm = Bitmap.createBitmap(bwidth, bheight,
  94.                     Bitmap.Config.ARGB_8888);
  95.             Canvas c = new Canvas(bm);
  96.             p.setColor(overlayBackgroundColor);
  97.             c.drawRect(0, 0, bwidth, bheight, p);
  98.             p.setColor(overlayTextColor);
  99.             c.drawText(text, -b.left + 1,
  100.                     (bheight / 2) - ((p.ascent() + p.descent()) / 2) + 1, p);
  101.             return bm;
  102.         }
  103.  
  104.         public void run() {
  105.             start = System.currentTimeMillis();
  106.             PorterDuffXfermode mode = new PorterDuffXfermode(
  107.                     PorterDuff.Mode.DST_OVER);
  108.             Bitmap bm;
  109.             int width;
  110.             int height;
  111.             Rect destRect;
  112.             Canvas c = null;
  113.             Paint p = new Paint();
  114.             String fps = "";
  115.             while (mRun) {
  116.                 if (surfaceDone) {
  117.                     try {
  118.                         c = mSurfaceHolder.lockCanvas();
  119.                         synchronized (mSurfaceHolder) {
  120.                             try {
  121.                                 bm = mIn.readMjpegFrame();
  122.                                 destRect = destRect(bm.getWidth(),
  123.                                         bm.getHeight());
  124.                                 c.drawColor(Color.BLACK);
  125.                                 c.drawBitmap(bm, null, destRect, p);
  126.                                 if (showFps) {
  127.                                     p.setXfermode(mode);
  128.                                     if (ovl != null) {
  129.                                         height = ((ovlPos & 1) == 1) ? destRect.top
  130.                                                 : destRect.bottom
  131.                                                         - ovl.getHeight();
  132.                                         width = ((ovlPos & <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" /> == <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" /> ? destRect.left
  133.                                                 : destRect.right
  134.                                                         - ovl.getWidth();
  135.                                         c.drawBitmap(ovl, width, height, null);
  136.                                     }
  137.                                     p.setXfermode(null);
  138.                                     frameCounter++;
  139.                                     if ((System.currentTimeMillis() - start) >= 1000) {
  140.                                         fps = String.valueOf(frameCounter)
  141.                                                 + "fps";
  142.                                         frameCounter = 0;
  143.                                         start = System.currentTimeMillis();
  144.                                         ovl = makeFpsOverlay(overlayPaint, fps);
  145.                                     }
  146.                                 }
  147.                             } catch (IOException e) {
  148.                                 System.out.println(e.toString());
  149.                             }
  150.                         }
  151.                     } finally {
  152.                         if (c != null)
  153.                             mSurfaceHolder.unlockCanvasAndPost(c);
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     private void init(Context context) {
  161.  
  162.         this.context = context;
  163.         SurfaceHolder holder = getHolder();
  164.         holder.addCallback(this);
  165.         thread = new MjpegViewThread(holder, context);
  166.         setFocusable(true);
  167.         if (!resume) {
  168.             resume = true;
  169.             overlayPaint = new Paint();
  170.             overlayPaint.setTextAlign(Paint.Align.LEFT);
  171.             overlayPaint.setTextSize(12);
  172.             overlayPaint.setTypeface(Typeface.DEFAULT);
  173.             overlayTextColor = Color.WHITE;
  174.             overlayBackgroundColor = Color.BLACK;
  175.             ovlPos = MjpegView.POSITION_LOWER_RIGHT;
  176.             displayMode = MjpegView.SIZE_STANDARD;
  177.             dispWidth = getWidth();
  178.             dispHeight = getHeight();
  179.         }
  180.     }
  181.  
  182.     public void startPlayback() {
  183.         if (mIn != null) {
  184.             mRun = true;
  185.             thread.start();
  186.         }
  187.     }
  188.  
  189.     public void resumePlayback() {
  190.         mRun = true;
  191.         init(context);
  192.         Log.i("AppLog", "resume");
  193.         thread.start();
  194.     }
  195.  
  196.     public void stopPlayback() {
  197.         mRun = false;
  198.         boolean retry = true;
  199.         while (retry) {
  200.             try {
  201.                 thread.join();
  202.                 retry = false;
  203.             } catch (InterruptedException e) {
  204.             }
  205.         }
  206.     }
  207.  
  208.     public MjpegView(Context context, AttributeSet attrs) {
  209.         super(context, attrs);
  210.         init(context);
  211.     }
  212.  
  213.     public void surfaceChanged(SurfaceHolder holder, int f, int w, int h) {
  214.         thread.setSurfaceSize(w, h);
  215.     }
  216.  
  217.     public void surfaceDestroyed(SurfaceHolder holder) {
  218.         surfaceDone = false;
  219.         stopPlayback();
  220.     }
  221.  
  222.     public MjpegView(Context context) {
  223.         super(context);
  224.         init(context);
  225.     }
  226.  
  227.     public void surfaceCreated(SurfaceHolder holder) {
  228.         surfaceDone = true;
  229.     }
  230.  
  231.     public void showFps(boolean b) {
  232.         showFps = b;
  233.     }
  234.  
  235.     public void setSource(MjpegInputStream source) {
  236.         mIn = source;
  237.         startPlayback();
  238.     }
  239.  
  240.     public void setOverlayPaint(Paint p) {
  241.         overlayPaint = p;
  242.     }
  243.  
  244.     public void setOverlayTextColor(int c) {
  245.         overlayTextColor = c;
  246.     }
  247.  
  248.     public void setOverlayBackgroundColor(int c) {
  249.         overlayBackgroundColor = c;
  250.     }
  251.  
  252.     public void setOverlayPosition(int p) {
  253.         ovlPos = p;
  254.     }
  255.  
  256.     public void setDisplayMode(int s) {
  257.         displayMode = s;
  258.     }
  259. }
Parsed in 0.069 seconds, using GeSHi 1.0.8.4


This code I got from different forums, the problem I have is that the image is very slow, particularly at 1 fps, when you should go to 20 which is what I sent the camera. Does anyone could help me with this problem?

Thank you very much for everything.

A greeting.
proyectohector
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jul 10, 2012 11:47 am

Re: MJPEG on Android anyone?

Postby antoniofajdiga » Thu May 16, 2013 10:58 am

can anyone share the complete android package ?
tnx
antoniofajdiga
Once Poster
Once Poster
 
Posts: 1
Joined: Thu May 16, 2013 10:56 am

Top
Previous

Return to Multimedia Problems

Who is online

Users browsing this forum: vivek19313 and 17 guests