问题描述
我修改从 Android和MJPEG 的MJPEG观众code使用的AsyncTask(工作从而对冰淇淋三明治(ICS),4.0.4)工作,这是我的code。
如果任何人有关于如何优化,清理,或做一些更合适的code,请让我知道的任何建议。有两个问题我想AP preciate帮助解决:
-
如果您有设备上的数据流,然后锁定屏幕和解锁屏幕不会继续播放,直至您杀害和恢复应用程序或旋转屏幕。我的所有尝试使用OnResume()做某件事,或者其他导致应用程序崩溃。
-
在我特别想获得的AsyncTask的回MjpegInputStream.java但没能得到那个工作。
MjpegActivity.java:
包com.demo.mjpeg;
进口java.io.IOException异常;
进口java.net.URI中;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口com.demo.mjpeg.MjpegView.MjpegInputStream;
进口com.demo.mjpeg.MjpegView.MjpegView;
进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Window;
进口android.view.WindowManager;
进口android.widget.Toast;
公共类MjpegActivity延伸活动{
私有静态最后字符串变量=MjpegActivity;
私人MjpegView MV;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//样本公众凸轮
字符串URL = "http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg/video.cgi?resolution=800x600&%3bdummy=1333689998337";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
MV =新MjpegView(本);
的setContentView(MV);
新DoRead()执行(URL);
}
公共无效的onPause(){
super.onPause();
mv.stopPlayback();
}
公共类DoRead扩展的AsyncTask<字符串,太虚,MjpegInputStream> {
保护MjpegInputStream doInBackground(字符串... URL){
// TODO:如果相机有认证处理它,不只是不工作
HTT presponse解析度= NULL;
DefaultHttpClient的HttpClient =新DefaultHttpClient();
Log.d(TAG,1.发送HTTP请求);
尝试 {
RES = httpclient.execute(新HTTPGET(URI.create(URL [0])));
Log.d(TAG2.请求完成后,状态=+ res.getStatusLine()的getStatus code());
如果(res.getStatusLine()的getStatus code()== 401){
//你必须关闭相机用户访问控制在此之前将工作
返回null;
}
返回新MjpegInputStream(res.getEntity()的getContent());
}赶上(ClientProtocolException E){
e.printStackTrace();
Log.d(TAG,请求失败,ClientProtocolException,E);
//错误连接到相机
}赶上(IOException异常E){
e.printStackTrace();
Log.d(TAG,请求失败,IOException异常,E);
//错误连接到相机
}
返回null;
}
保护无效onPostExecute(MjpegInputStream结果){
mv.setSource(结果);
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
mv.showFps(真正的);
}
}
}
MjpegInputStream.java:
包com.demo.mjpeg.MjpegView;
进口java.io.BufferedInputStream中;
进口java.io.ByteArrayInputStream中;
进口java.io.DataInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.util.Properties;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.util.Log;
公共类MjpegInputStream扩展的DataInputStream {
私有静态最后字符串变量=MjpegInputStream;
私人最终的byte [] SOI_MARKER = {(字节)0xFF的,(字节)为0xD8};
私人最终的byte [] EOF_MARKER = {(字节)0xFF的,(字节)0xD9};
私人最终字符串CONTENT_LENGTH =内容长度;
私人最终静态INT HEADER_MAX_LENGTH = 100;
私人最终静态INT FRAME_MAX_LENGTH = 40000 + HEADER_MAX_LENGTH;
私人诠释mContentLength = -1;
公共MjpegInputStream(InputStream中的){
超级(新的BufferedInputStream(在,FRAME_MAX_LENGTH));
}
私人诠释getEndOfSeqeunce(在的DataInputStream,byte []的顺序)抛出IOException异常{
INT seqIndex = 0;
字节℃;
的for(int i = 0; I< FRAME_MAX_LENGTH;我++){
C =(字节)in.readUnsignedByte();
如果(C ==序列[seqIndex]){
seqIndex ++;
如果(seqIndex == sequence.length){
返回I + 1;
}
} 其他 {
seqIndex = 0;
}
}
返回-1;
}
私人诠释getStartOfSequence(在的DataInputStream,byte []的顺序)抛出IOException异常{
INT端= getEndOfSeqeunce(中,序列);
返回(完℃下)? (-1):(结束 - sequence.length);
}
私人诠释parseContentLength(byte []的headerBytes)抛出IOException异常,NumberFormatException的{
ByteArrayInputStream的headerIn =新ByteArrayInputStream的(headerBytes);
属性道具=新特性();
props.load(headerIn);
返回的Integer.parseInt(props.getProperty(CONTENT_LENGTH));
}
公共位图readMjpegFrame()抛出IOException异常{
标记(FRAME_MAX_LENGTH);
INT headerLen = getStartOfSequence(这一点,SOI_MARKER);
复位();
byte []的头=新的字节[headerLen]
的readFully(头);
尝试 {
mContentLength = parseContentLength(头);
}赶上(NumberFormatException的NFE){
nfe.getStackTrace();
Log.d(TAG,抓NumberFormatException的打击,NFE);
mContentLength = getEndOfSeqeunce(这一点,EOF_MARKER);
}
复位();
byte []的frameData =新的字节[mContentLength]
的skipBytes(headerLen);
的readFully(frameData);
返回BitmapFactory.de codeStream(新ByteArrayInputStream的(frameData));
}
}
MjpegView.java:
包com.demo.mjpeg.MjpegView;
进口java.io.IOException异常;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.PorterDuff;
进口android.graphics.PorterDuffXfermode;
进口android.graphics.Rect;
进口android.graphics.Typeface;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
公共类MjpegView扩展了SurfaceView实现SurfaceHolder.Callback {
私有静态最后字符串变量=MjpegView;
公共最后静态INT POSITION_UPPER_LEFT = 9;
公共最后静态INT POSITION_UPPER_RIGHT = 3;
公共最后静态INT POSITION_LOWER_LEFT = 12;
公共最后静态INT POSITION_LOWER_RIGHT = 6;
公共最后静态INT SIZE_STANDARD = 1;
公共最后静态INT SIZE_BEST_FIT = 4;
公共最后静态INT SIZE_FULLSCREEN = 8;
私人MjpegViewThread线;
私人MjpegInputStream分钟= NULL;
私人布尔showFps = FALSE;
私人布尔mRun = FALSE;
私人布尔surfaceDone = FALSE;
私人油漆overlayPaint;
私人诠释overlayTextColor;
私人诠释overlayBackgroundColor;
私人诠释ovlPos;
私人诠释dispWidth;
私人诠释dispHeight;
私人诠释DISPLAYMODE;
公共类MjpegViewThread继承Thread {
私人SurfaceHolder mSurfaceHolder;
私人诠释frameCounter = 0;
私人长期的开始;
私人位图OVL;
公共MjpegViewThread(SurfaceHolder surfaceHolder,上下文语境){
mSurfaceHolder = surfaceHolder;
}
私人矩形destRect(INT宝马,INT BMH){
INT tempx;
INT tempy;
如果(DISPLAYMODE == MjpegView.SIZE_STANDARD){
tempx =(dispWidth / 2) - (BMW / 2);
tempy =(dispHeight / 2) - (BMH / 2);
返回新的矩形(tempx,tempy,宝马+ tempx,BMH + tempy);
}
如果(DISPLAYMODE == MjpegView.SIZE_BEST_FIT){
浮动bmasp =(浮点)宝马/(浮点)BMH;
宝马= dispWidth;
BMH =(INT)(dispWidth / bmasp);
如果(BMH> dispHeight){
BMH = dispHeight;
宝马=(INT)(dispHeight * bmasp);
}
tempx =(dispWidth / 2) - (BMW / 2);
tempy =(dispHeight / 2) - (BMH / 2);
返回新的矩形(tempx,tempy,宝马+ tempx,BMH + tempy);
}
如果(DISPLAYMODE == MjpegView.SIZE_FULLSCREEN){
返回新的矩形(0,0,dispWidth,dispHeight);
}
返回null;
}
公共无效setSurfaceSize(INT宽度,高度INT){
同步(mSurfaceHolder){
dispWidth =宽度;
dispHeight =高度;
}
}
私人位图makeFpsOverlay(涂料P,字符串文本){
矩形B =新的矩形();
p.getTextBounds(文字,0,text.length(),二);
INT bwidth = b.width()+ 2;
INT bheight = b.height()+ 2;
位图BM = Bitmap.createBitmap(bwidth,bheight,Bitmap.Config.ARGB_8888);
帆布C =新的Canvas(BM);
p.setColor(overlayBackgroundColor);
c.drawRect(0,0,bwidth,bheight,p)的;
p.setColor(overlayTextColor);
c.drawText(文字,-b.left + 1,(bheight / 2) - ((p.ascent()+ p.descent())/ 2)1,p)的;
返回BM;
}
公共无效的run(){
开始= System.currentTimeMillis的();
PorterDuffXfermode模式=新PorterDuffXfermode(PorterDuff.Mode.DST_OVER);
位图BM;
INT宽度;
INT高度;
矩形destRect;
帆布C = NULL;
涂料P =新的油漆();
字符串FPS;
而(mRun){
如果(surfaceDone){
尝试 {
C = mSurfaceHolder.lockCanvas();
同步(mSurfaceHolder){
尝试 {
BM = mIn.readMjpegFrame();
destRect = destRect(bm.getWidth(),bm.getHeight());
c.drawColor(Color.BLACK);
c.drawBitmap(BM,空,destRect,P);
如果(showFps){
p.setXfermode(模式);
如果(OVL!= NULL){
高度=((ovlPos&安培; 1)== 1)? destRect.top:destRect.bottom-ovl.getHeight();
宽度=((ovlPos和8)== 8)? destRect.left:destRect.right -ovl.getWidth();
c.drawBitmap(OVL,宽度,高度,NULL);
}
p.setXfermode(空);
frameCounter ++;
如果((System.currentTimeMillis的() - 启动)> = 1000){
FPS =将String.valueOf(frameCounter)+FPS;
frameCounter = 0;
开始= System.currentTimeMillis的();
OVL = makeFpsOverlay(overlayPaint,FPS);
}
}
}赶上(IOException异常E){
e.getStackTrace();
Log.d(TAG,抓IOException异常命中运行,E);
}
}
} 最后 {
如果(C!= NULL){
mSurfaceHolder.unlockCanvasAndPost(C);
}
}
}
}
}
}
私人无效的init(上下文的背景下){
SurfaceHolder支架= getHolder();
holder.addCallback(本);
线程=新MjpegViewThread(架,背景);
setFocusable(真正的);
overlayPaint =新的油漆();
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();
}
公共无效startPlayback(){
如果(MIN!= NULL){
mRun = TRUE;
thread.start();
}
}
公共无效stopPlayback(){
mRun = FALSE;
布尔重试= TRUE;
而(重试){
尝试 {
的Thread.join();
重试= FALSE;
}赶上(InterruptedException异常E){
e.getStackTrace();
Log.d(TAG,抓IOException异常命中stopPlayback,E);
}
}
}
公共MjpegView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);的init(上下文);
}
公共无效surfaceChanged(SurfaceHolder架,诠释楼INT W,INT高){
thread.setSurfaceSize(W,H);
}
公共无效surfaceDestroyed(SurfaceHolder持有者){
surfaceDone = FALSE;
stopPlayback();
}
公共MjpegView(上下文的背景下){
超(上下文);
的init(上下文);
}
公共无效surfaceCreated(SurfaceHolder持有者){
surfaceDone = TRUE;
}
公共无效showFps(布尔B){
showFps = B;
}
公共无效的SetSource(MjpegInputStream源){
MIN =来源;
startPlayback();
}
公共无效setOverlayPaint(涂料P){
overlayPaint = P;
}
公共无效setOverlayTextColor(INT C){
overlayTextColor = C;
}
公共无效setOverlayBackgroundColor(INT C){
overlayBackgroundColor = C;
}
公共无效setOverlayPosition(INT P){
ovlPos = P;
}
公共无效setDisplayMode(int类型){
DISPLAYMODE =秒;
}
}
这将是对新手有所帮助,如果您要访问您的网络摄像机有一个用户名和密码,您可能想要将它添加到您的 DefaultHttpClient
和上面的code将努力为需要认证摄像头
CredentialsProvider商=新BasicCredentialsProvider();
UsernamePasswordCredentials凭证=新UsernamePasswordCredentials(yourusername,你的密码);
provider.setCredentials(AuthScope.ANY,证书);
DefaultHttpClient的HttpClient =新DefaultHttpClient();
httpclient.setCredentialsProvider(供应商);
I modified the MJPEG viewer code from Android and MJPEG to work using an AsyncTask (and thus work on Ice Cream Sandwich (ICS), 4.0.4) and here is my code.
If anyone has any suggestions on how to optimize, cleanup, or do something more proper with the code please let me know. Two issues I'd appreciate help addressing:
If you have the device on a stream then lock the screenand unlock the screen it does not resume playing until you eitherkill and resume the app or rotate the screen. All my attempts at using OnResume() to do something or other resulted in app crashes.
In particular I'd like to get the AsyncTask back inMjpegInputStream.java but was not able to get that to work.
MjpegActivity.java:
package com.demo.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.demo.mjpeg.MjpegView.MjpegInputStream;
import com.demo.mjpeg.MjpegView.MjpegView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class MjpegActivity extends Activity {
private static final String TAG = "MjpegActivity";
private MjpegView mv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//sample public cam
String URL = "http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg/video.cgi?resolution=800x600&%3bdummy=1333689998337";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mv = new MjpegView(this);
setContentView(mv);
new DoRead().execute(URL);
}
public void onPause() {
super.onPause();
mv.stopPlayback();
}
public class DoRead extends AsyncTask<String, Void, MjpegInputStream> {
protected MjpegInputStream doInBackground(String... url) {
//TODO: if camera has authentication deal with it and don't just 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 this will work
return null;
}
return new MjpegInputStream(res.getEntity().getContent());
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.d(TAG, "Request failed-ClientProtocolException", e);
//Error connecting to camera
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "Request failed-IOException", e);
//Error connecting to camera
}
return null;
}
protected void onPostExecute(MjpegInputStream result) {
mv.setSource(result);
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
mv.showFps(true);
}
}
}
MjpegInputStream.java:
package com.demo.mjpeg.MjpegView;
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 MjpegInputStream extends DataInputStream {
private static final String TAG = "MjpegInputStream";
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 MjpegInputStream(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 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));
}
}
MjpegView.java:
package com.demo.mjpeg.MjpegView;
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.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 MjpegInputStream 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) {
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 / 2) - (bmh / 2);
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.DST_OVER);
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);
}
}
} finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
private void init(Context context) {
SurfaceHolder holder = getHolder();
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(MjpegInputStream 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;
}
}
It'll be helpful for the newbies that if you want to access your ip camera having a username or password , you might want to add this to your DefaultHttpClient
and the above code will work for cameras that require authentication
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("yourusername", "yourpassword");
provider.setCredentials(AuthScope.ANY, credentials);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(provider);
这篇关于使用AsyncTask的安卓ICS和MJPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!