video - how to return an object into a surface view android -
i following code on stack android ics , mjpeg using asynctask
which stream mjpeg java (code below well). seems happening mjpegview class creating mjpeg object , forcing main activity set content view. there way can throw surfaceholder in mjpegviewer class surface view defined in xml can other things? tried create surface view , replace in mjpeg viewer class crashes.. have idea of how can put own surface view?
main act package com.example.mjpeg;
import java.io.ioexception; import java.net.uri; 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 com.example.mjpeg.mjpegstream; import com.example.mjpeg.mjpegview; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.surfaceview; import android.view.window; import android.view.windowmanager; import android.widget.toast; public class mainactivity extends activity { private static final string tag = "mainactivity"; private mjpegview mv; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //sample public cam string url = "http://tfdsfdfdsfd.cgi"; // mv = new mjpegview(this); setcontentview(r.layout.activity_main); setcontentview(mv); //set preview activity content // setcontentview(mv); new doread().execute(url); } public void onpause() { super.onpause(); mv.stopplayback(); } public class doread extends asynctask<string, void, mjpegstream> { protected mjpegstream doinbackground(string... url) { //todo: if camera has authentication deal , don't not work httpresponse res = null; defaulthttpclient httpclient = new defaulthttpclient(); log.d(tag, "1. sending http request"); try { res = httpclient.execute(new httpget(uri.create(url[0]))); log.d(tag, "2. request finished, status = " + res.getstatusline().getstatuscode()); if(res.getstatusline().getstatuscode()==401){ //you must turn off camera user access control before work return null; } return new mjpegstream(res.getentity().getcontent()); } catch (clientprotocolexception e) { e.printstacktrace(); log.d(tag, "request failed-clientprotocolexception", e); //error connecting camera } catch (ioexception e) { e.printstacktrace(); log.d(tag, "request failed-ioexception", e); //error connecting camera } return null; } protected void onpostexecute(mjpegstream result) { mv.setsource(result); mv.setdisplaymode(mjpegview.size_best_fit); mv.showfps(true); } } }
mjpegviewer class
package com.example.mjpeg; import java.io.ioexception; import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.pixelformat; import android.graphics.porterduff; import android.graphics.porterduffxfermode; import android.graphics.rect; import android.graphics.typeface; import android.util.attributeset; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; public class mjpegview extends surfaceview implements surfaceholder.callback { private static final string tag = "mjpegview"; 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 mjpegviewthread thread; private mjpegstream 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 class mjpegviewthread extends thread { private surfaceholder msurfaceholder; private int framecounter = 0; private long start; private bitmap ovl; public mjpegviewthread(surfaceholder surfaceholder, context context) { surfaceview camsurface = (surfaceview) findviewbyid(r.id.camsurface); surfaceholder msurfaceholder = camsurface.getholder(); msurfaceholder = surfaceholder; } private rect destrect(int bmw, int bmh) { int tempx; int tempy; if (displaymode == mjpegview.size_standard) { tempx = (dispwidth / 2) - (bmw / 2); tempy = (dispheight / 2) - (bmh / 2); return new rect(tempx, tempy, bmw + tempx, bmh + tempy); } if (displaymode == mjpegview.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 / 4) - (bmh / 2); tempy = 0; return new rect(tempx, tempy, bmw + tempx, bmh + tempy); } if (displaymode == mjpegview.size_fullscreen){ return new rect(0, 0, dispwidth, dispheight); } return null; } 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.src_out); bitmap bm; int width; int height; rect destrect; canvas c = null; paint p = new paint(); string fps; while (mrun) { if(surfacedone) { try { c = msurfaceholder.lockcanvas(); synchronized (msurfaceholder) { try { bm = min.readmjpegframe(); destrect = destrect(bm.getwidth(),bm.getheight()); // c.drawcolor(color.black); c.drawbitmap(bm, null, destrect, p); if(showfps) { p.setxfermode(mode); if(ovl != null) { height = ((ovlpos & 1) == 1) ? destrect.top : destrect.bottom-ovl.getheight(); width = ((ovlpos & 8) == 8) ? destrect.left : destrect.right -ovl.getwidth(); c.drawbitmap(ovl, width, height, null); } p.setxfermode(null); framecounter++; if((system.currenttimemillis() - start) >= 1000) { fps = string.valueof(framecounter)+" fps"; framecounter = 0; start = system.currenttimemillis(); ovl = makefpsoverlay(overlaypaint, fps); } } } catch (ioexception e) { e.getstacktrace(); log.d(tag, "catch ioexception hit in run", e); } } } { if (c != null) { msurfaceholder.unlockcanvasandpost(c); } } } } } } private void init(context context) { surfaceholder holder = getholder(); getholder().setformat(pixelformat.translucent); holder.addcallback(this); thread = new mjpegviewthread(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 = mjpegview.position_lower_right; displaymode = mjpegview.size_standard; dispwidth = getwidth(); dispheight = getheight(); } public void startplayback() { if(min != null) { mrun = true; thread.start(); } } public void stopplayback() { mrun = false; boolean retry = true; while(retry) { try { thread.join(); retry = false; } catch (interruptedexception e) { e.getstacktrace(); log.d(tag, "catch ioexception hit in stopplayback", e); } } } public mjpegview(context context, attributeset attrs) { super(context, attrs); init(context); } public void surfacechanged(surfaceholder holder, int f, int w, int h) { thread.setsurfacesize(w, h); } public void surfacedestroyed(surfaceholder holder) { surfacedone = false; stopplayback(); } public mjpegview(context context) { super(context); init(context); } public void surfacecreated(surfaceholder holder) { surfacedone = true; } public void showfps(boolean b) { showfps = b; } public void setsource(mjpegstream 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; } }
mjpeg stream class
package com.example.mjpeg; import java.io.bufferedinputstream; import java.io.bytearrayinputstream; import java.io.datainputstream; import java.io.ioexception; import java.io.inputstream; import java.util.properties; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.util.log; public class mjpegstream extends datainputstream { private static final string tag = "mjpegstream"; 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 mjpegstream(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; < frame_max_length; i++) { c = (byte) in.readunsignedbyte(); if(c == sequence[seqindex]) { seqindex++; if(seqindex == sequence.length) { return + 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 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) { nfe.getstacktrace(); log.d(tag, "catch numberformatexception hit", nfe); mcontentlength = getendofseqeunce(this, eof_marker); } reset(); byte[] framedata = new byte[mcontentlength]; skipbytes(headerlen); readfully(framedata); return bitmapfactory.decodestream(new bytearrayinputstream(framedata)); } }
any appreciated thank you
mv = new mjpegview(this);
// setcontentview(r.layout.activity_main); setcontentview(mv); //set preview activity content
view stolenview = mv; // set own view setcontentview(r.layout.activity_main); view view =(findviewbyid(r.id.vids)); ((viewgroup) view).addview(stolenview);
there nasty trick can try. didn't try self give idea.
steal view mjpegview
. first thing let him set content view, take this:
// obtain mjpegview reference view stolenview = getwindow().getdecorview().findviewbyid(android.r.id.content); // set own view setcontentview(r.layout.activity_main);
and add view.
(parentview)(findviewbyid(r.id.mjpegviewcontainer)).addview(stolenview);
Comments
Post a Comment