Bonjour,
je suis entrain de faire mon projet de fin d'étude (vidéo surveillance mobile sous android)
j'ai réussi à faire la visualisation d'une caméra ip selon le choisi à travers une listeview de caméra ip ( récupéré de la base de données)
mais si le choisie (de 2 camera jusqu'a 6) il affiche uniquement le flux la dernier cirulent en boucle entre les différentes position (nombre de position est égale à celui de caméra choisie), mon probléme se manifeste dans le web service qui recupére l'adresse ip de tout les caméras choisie dans la classe
public static Mjpegsample read(String url)
mais n'affiche pas le flux à cause de la classe
public Bitmap readMjpegFrame() throws IOException
je n'arrive pas la adapté correctemnet, y a -t- qlq qui peut m'aider. merci d'avance
voila mon code:
classe visualiser flux:
classe MjpegMultiView
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 package com.andro.surv; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class visualiserflux_j extends Activity implements OnClickListener{ //public MjpegView mv; //public Mjpegsample mpegs ; public MjpegMultiView mmv ; boolean affiche = false; private Button r1; ListView listV; private int checkedItem=0; String Hostrecuper; String Portrecuper; cameraBDD cameraBdd = new cameraBDD(this); public ArrayList<String> ListHostCam = new ArrayList<String>(); public ArrayList<String> ListPortCam = new ArrayList<String>(); public ArrayList<String> ListUsernameCam = new ArrayList<String>(); public ArrayList<String> ListPswCam = new ArrayList<String>(); int k=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.visualiserflux); r1 = (Button) findViewById(R.id.bVTR); r1.setOnClickListener(this); /********** Actualisation de la liste des caméras *********/ try{ cameraBdd.open(); ArrayList<HashMap<String, String>> listItem = cameraBdd.mapCameras(); SimpleAdapter mSchedule = new SimpleAdapter(getBaseContext(), listItem, R.layout.affichageitem1, new String[] {"img", "id", "name", "host"}, new int[] {R.id.img, R.id.id, R.id.type, R.id.host}); listV.setAdapter(mSchedule); cameraBdd.close(); }catch(Exception e){ Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } listV =(ListView) findViewById(R.id.listV); cameraBdd.open(); ArrayList<HashMap<String, String>> listItem = cameraBdd.mapCameras(); SimpleAdapter mSchedule = new SimpleAdapter(this.getBaseContext(), listItem, R.layout.affichageitem1, new String[] {"img", "id", "name", "host"}, new int[] {R.id.img, R.id.id, R.id.type, R.id.host}); listV.setAdapter(mSchedule); cameraBdd.close(); //Associer l'venemetn lorsque on clique sur un contact listV.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { try{ HashMap<String, String> map = (HashMap<String, String>) listV.getItemAtPosition(position); checkedItem=position; listV.setItemChecked(position, true); ListHostCam.add(map.get("host")); ListPortCam.add(map.get("port")); ListUsernameCam.add(map.get("username")); ListPswCam.add(map.get("password")); }catch(Exception e){ Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } public void ConnectCam(String username,String psw,String IP,String PORT, int nbcam) { String URL; if ((username=="")&&(psw=="")) // camera public n'ayant pas un username et un psw { URL = "http://"+IP+":"+PORT+"/axis-cgi/" +"mjpg/video.cgi?resolution=640x480"; } else URL = "http://"+username+":"+psw+"@"+IP+":"+PORT+"/axis-cgi/" +"mjpg/video.cgi?resolution=640x480"; mmv= new MjpegMultiView(getBaseContext()); //Permét d'afficher l'objet multivisualisation// setContentView(mmv); mmv.setDisplayNbCam(nbcam); mmv.setSource( Mjpegsample.read(URL.trim())); //LA SOURCE DE MJPEG mmv.setDisplayMode(MjpegMultiView.SIZE_BEST_FIT); mmv.showFps(false); affiche = true; } @Override public void onClick(View arg0) { for(int j=0; j<ListHostCam.size();j++) ConnectCam(ListUsernameCam.get(j),ListPswCam.get(j),ListHostCam.get(j),ListPortCam.get(j),ListHostCam.size() ); } }
classe Mjpegsample:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 package com.andro.surv; import java.io.IOException; import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.Toast; public class MjpegMultiView extends SurfaceView implements SurfaceHolder.Callback { public MjpegMultiView(Context context) { super(context); init(context); } public MjpegMultiView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public final static int POSITION_UPPER_LEFT = 9; public final static int POSITION_UPPER_RIGHT = 3; public final static int POSITION_LOWER_LEFT = 12; public final static int POSITION_LOWER_RIGHT = 6; public final static int SIZE_STANDARD = 1; public final static int SIZE_BEST_FIT = 4; public final static int SIZE_FULLSCREEN = 8; private static final String TAG = "MOT_MyActivity"; /* define log tag*/ private MjpegMultiViewThread thread; private Mjpegsample mIn = null; private boolean showFps = false; private boolean mRun = false; private boolean surfaceDone = false; private Paint overlayPaint; private int overlayTextColor; private int overlayBackgroundColor; private int ovlPos; private int dispWidth; private int dispHeight; private int displayMode; public int nbCamVisualiser; public ArrayList<Rect> ListCoordView = new ArrayList<Rect>(); int k=0; public class MjpegMultiViewThread extends Thread { private SurfaceHolder mSurfaceHolder; private int frameCounter = 0; private long start; private Bitmap ovl; public MjpegMultiViewThread(SurfaceHolder surfaceHolder, Context context) { mSurfaceHolder = surfaceHolder; } public void setSurfaceSize(int width, int height) { synchronized(mSurfaceHolder) { dispWidth = width; dispHeight = height; } } private Bitmap makeFpsOverlay(Paint p, String text) { Rect b = new Rect(); p.getTextBounds(text, 0, text.length(), b); int bwidth = b.width()+2; int bheight = b.height()+2; Bitmap bm = Bitmap.createBitmap(bwidth, bheight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bm); p.setColor(overlayBackgroundColor); c.drawRect(0, 0, bwidth, bheight, p); p.setColor(overlayTextColor); c.drawText(text, -b.left+1, (bheight/2)-((p.ascent()+p.descent())/2)+1, p); return bm; } ///////////////////////////////////////////// public void run() { start = System.currentTimeMillis(); PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_OVER); Bitmap bm; int width; int height; ArrayList<Rect> destRect; Canvas c = null; Paint p = new Paint(); String fps = ""; int nbcamVis; while (mRun) { if(surfaceDone) { try { c = mSurfaceHolder.lockCanvas(); synchronized (mSurfaceHolder) { try { bm = mIn.readMjpegFrame(); //.get(k); destRect = destRect(bm.getWidth(),bm.getHeight(),getDisplayNbCam() ); c.drawColor(Color.BLACK); // c.drawBitmap(bm, null, p); //for(int k=0; k<ListCoordView.size();k++) // { c.drawBitmap(bm, null, destRect.get(k), p); //} k++; } catch (IOException e) {} } } finally { if (c != null) mSurfaceHolder.unlockCanvasAndPost(c); } } } } } public ArrayList<Rect> destRect(int bmw, int bmh, int nbCam) { int tempx; int tempy; if (displayMode == MjpegMultiView.SIZE_BEST_FIT) { float bmasp = (float) bmw / (float) bmh; bmw = dispWidth; bmh = (int) (dispWidth / bmasp); if (bmh > dispHeight) { bmh = dispHeight; bmw = (int) (dispHeight * bmasp); } tempx = (dispWidth / 2) - (bmw / 2); tempy = (dispHeight / 2) - (bmh / 2); if (nbCam ==1) { ListCoordView.add( new Rect(tempx, tempy, bmw + tempx, bmh + tempy)); return ListCoordView;} if (nbCam ==2) { ListCoordView.add(new Rect(tempx, tempy, (bmw/2) + tempx, bmh+ tempy)); ListCoordView.add(new Rect((bmw/2) + tempx,tempy, bmw + tempx, bmh+ tempy)); return ListCoordView;} if (nbCam ==3 ) {ListCoordView.add(new Rect(tempx, tempy, (bmw/2) + tempx,bmh/2+ tempy)); ListCoordView.add(new Rect((bmw/2) + tempx,tempy, bmw + tempx, bmh/2+ tempy)); ListCoordView.add(new Rect(tempx,bmh/2+ tempy, tempx+bmw/2, tempy+bmh)); return ListCoordView;} if (nbCam ==4) { ListCoordView.add(new Rect(tempx, tempy, (bmw/2) + tempx,bmh/2+ tempy)); ListCoordView.add(new Rect((bmw/2) + tempx,tempy, bmw + tempx, bmh/2+ tempy)); ListCoordView.add(new Rect(tempx,bmh/2+ tempy, tempx+bmw/2, tempy+bmh)); ListCoordView.add(new Rect(tempx+bmw/2,tempy+bmh/2, tempx+bmw,tempy+bmh)); return ListCoordView;} if (nbCam==5) {ListCoordView.add(new Rect(tempx, tempy,tempx+(bmw/3),tempy+bmh/2)); ListCoordView.add(new Rect(tempx+(bmw/3),tempy,tempx+bmw*2/3,tempy+bmh/2)); ListCoordView.add(new Rect(tempx+bmw*2/3,tempy,tempx+bmw,tempy+bmh/2)); ListCoordView.add(new Rect(tempx,tempy+bmh/2,tempx+bmw/3,tempy+bmh)); ListCoordView.add(new Rect(tempx+bmw/3,tempy+bmh/2,tempx+bmw*2/3,tempy+bmh)); return ListCoordView; } if (nbCam==6) { ListCoordView.add(new Rect(tempx, tempy,tempx+(bmw/3),tempy+bmh/2)); ListCoordView.add(new Rect(tempx+(bmw/3),tempy,tempx+bmw*2/3,tempy+bmh/2)); ListCoordView.add(new Rect(tempx+bmw*2/3,tempy,tempx+bmw,tempy+bmh/2)); ListCoordView.add(new Rect(tempx,tempy+bmh/2,tempx+bmw/3,tempy+bmh)); ListCoordView.add(new Rect(tempx+bmw/3,tempy+bmh/2,tempx+bmw*2/3,tempy+bmh)); ListCoordView.add(new Rect(tempx+bmw*2/3,tempy+bmh/2,tempx+bmw,tempy+bmh)); return ListCoordView;} } return null; } private void init(Context context) { SurfaceHolder holder = getHolder(); holder.addCallback(this); thread = new MjpegMultiViewThread(holder, context); setFocusable(true); overlayPaint = new Paint(); overlayPaint.setTextAlign(Paint.Align.LEFT); overlayPaint.setTextSize(12); overlayPaint.setTypeface(Typeface.DEFAULT); overlayTextColor = Color.WHITE; overlayBackgroundColor = Color.BLACK; ovlPos = MjpegMultiView.POSITION_LOWER_RIGHT; displayMode = MjpegMultiView.SIZE_STANDARD; dispWidth = getWidth(); dispHeight = getHeight(); } public void startPlayback() { // Mon code à éxécuter if(mIn != null) { mRun = true; surfaceDone= true; // a été ajouté thread.start();} } public void stopPlayback() { mRun = false; boolean retry = true; while(retry) { try { thread.join(); retry = false; } catch (InterruptedException e) {} } } public void surfaceChanged(SurfaceHolder holder, int f, int w, int h) { thread.setSurfaceSize(w, h); } public void surfaceDestroyed(SurfaceHolder holder) { surfaceDone = false; stopPlayback(); } public void surfaceCreated(SurfaceHolder holder) { surfaceDone = true; } public void showFps(boolean b) { showFps = b;} public void setSource(Mjpegsample source) { mIn =source; startPlayback(); } public void setOverlayPaint(Paint p) { overlayPaint = p; } public void setOverlayTextColor(int c) { overlayTextColor = c; } public void setOverlayBackgroundColor(int c) { overlayBackgroundColor = c; } public void setOverlayPosition(int p) { ovlPos = p; } public void setDisplayMode(int s) { displayMode = s; } public void setDisplayNbCam(int nbCam){ nbCamVisualiser= nbCam;} public int getDisplayNbCam(){ return nbCamVisualiser;} }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 package com.andro.surv; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.ArrayList; import java.util.Properties; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Mjpegsample extends DataInputStream { private final byte[] SOI_MARKER = { (byte) 0xFF, (byte) 0xD8 }; private final byte[] EOF_MARKER = { (byte) 0xFF, (byte) 0xD9 }; private final String CONTENT_LENGTH = "Content-Length"; private final static int HEADER_MAX_LENGTH = 100; private final static int FRAME_MAX_LENGTH = 40000 + HEADER_MAX_LENGTH; private int mContentLength = -1; public String urlcam; public ArrayList<Bitmap> ListBitmap = new ArrayList<Bitmap>(); public static Mjpegsample read(String url) { HttpResponse res; DefaultHttpClient httpclient = new DefaultHttpClient(); try { res = httpclient.execute(new HttpGet(URI.create(url))); return new Mjpegsample(res.getEntity().getContent()); } catch (ClientProtocolException e) { } catch (IOException e) {} return null; } public Mjpegsample(InputStream in) { super(new BufferedInputStream(in, FRAME_MAX_LENGTH)); } private int getEndOfSeqeunce(DataInputStream in, byte[] sequence) throws IOException { int seqIndex = 0; byte c; for(int i=0; i < FRAME_MAX_LENGTH; i++) { c = (byte) in.readUnsignedByte(); if(c == sequence[seqIndex]) { seqIndex++; if(seqIndex == sequence.length) return i + 1; } else seqIndex = 0; } return -1; } private int getStartOfSequence(DataInputStream in, byte[] sequence) throws IOException { int end = getEndOfSeqeunce(in, sequence); return (end < 0) ? (-1) : (end - sequence.length); } private int parseContentLength(byte[] headerBytes) throws IOException, NumberFormatException { ByteArrayInputStream headerIn = new ByteArrayInputStream(headerBytes); Properties props = new Properties(); props.load(headerIn); return Integer.parseInt(props.getProperty(CONTENT_LENGTH)); } // public ArrayList<Bitmap> public Bitmap readMjpegFrame() throws IOException { mark(FRAME_MAX_LENGTH); int headerLen = getStartOfSequence(this, SOI_MARKER); reset(); byte[] header = new byte[headerLen]; readFully(header); try { mContentLength = parseContentLength(header); } catch (NumberFormatException nfe) { mContentLength = getEndOfSeqeunce(this, EOF_MARKER); } reset(); byte[] frameData = new byte[mContentLength]; skipBytes(headerLen); readFully(frameData); return BitmapFactory.decodeStream(new ByteArrayInputStream(frameData));}
Partager