我对android编程很新,但我学的很快.所以我在这里找到了一段有趣的代码:http://code.google.com/p/camdroiduni/source/browse/trunk/code/eclipse_workspace/camdroid/src/de/aes/camdroid/CameraView.java
它是关于从设备的相机到浏览器的实时流媒体.
但我想知道代码实际上是如何工作的.
这些是我想要了解的内容:
1)它们如何流式传输到webbrowser.我知道他们将index.html文件发送到设备的ip地址(在wifi上),该文件每秒重新加载页面.但他们如何使用套接字将index.html文件发送到所需的 IP地址?
2)http://code.google.com/p/camdroiduni/wiki/Status#save_pictures_frequently,他们在这里提到他们正在使用视频,但我仍然相信他们拍照并发送它们,因为我没有看到任何地方的媒体记录.
现在我的问题是他们如何继续发送并将这些图像保存到SD文件夹中(我认为).我认为这是用这个代码完成的,但它是如何工作的.与c.takepicture一样,需要很长时间才能保存并再次开始预览,因此不能选择直播.
public synchronized byte[] getPicture() {
try {
while (!isPreviewOn) wait();
isDecoding = true;
mCamera.setOneShotPreviewCallback(this);
while (isDecoding) wait();
} catch (Exception e) {
return null;
}
return mCurrentFrame;
}
private LayoutParams calcResolution (int origWidth, int origHeight, int aimWidth, int aimHeight) {
double origRatio = (double)origWidth/(double)origHeight;
double aimRatio = (double)aimWidth/(double)aimHeight;
if (aimRatio>origRatio)
return new LayoutParams(origWidth,(int)(origWidth/aimRatio));
else
return new LayoutParams((int)(origHeight*aimRatio),origHeight);
}
private void raw2jpg(int[] rgb, byte[] raw, int width, int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y=0;
if(yp < raw.length) {
y = (0xff & ((int) raw[yp])) - 16;
}
if (y < 0) y = 0;
if ((i & 1) == 0) {
if(uvp<raw.length) {
v = (0xff & raw[uvp++]) - 128;
u = (0xff & raw[uvp++]) - 128;
}
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0) r = 0; else if (r > 262143) r = 262143;
if (g < 0) g = 0; else if (g > 262143) g = 262143;
if (b < 0) b = 0; else if (b > 262143) b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) &
0xff0000) | ((g >> 2) &
0xff00) | ((b >> 10) &
0xff);
}
}
}
@Override
public synchronized void onPreviewFrame(byte[] data, Camera camera) {
int width = mSettings.PictureW() ;
int height = mSettings.PictureH();
// API 8 and above
// YuvImage yuvi = new YuvImage(data, ImageFormat.NV21 , width, height, null);
// Rect rect = new Rect(0,0,yuvi.getWidth() ,yuvi.getHeight() );
// OutputStream out = new ByteArrayOutputStream();
// yuvi.compressToJpeg(rect, 10, out);
// byte[] ref = ((ByteArrayOutputStream)out).toByteArray();
// API 7
int[] temp = new int[width*height];
OutputStream out = new ByteArrayOutputStream();
// byte[] ref = null;
Bitmap bm = null;
raw2jpg(temp, data, width, height);
bm = Bitmap.createBitmap(temp, width, height, Bitmap.Config.RGB_565);
bm.compress(CompressFormat.JPEG, mSettings.PictureQ(), out);
/*ref*/mCurrentFrame = ((ByteArrayOutputStream)out).toByteArray();
// mCurrentFrame = new byte[ref.length];
// System.arraycopy(ref, 0, mCurrentFrame, 0, ref.length);
isDecoding = false;
notify();
}
Run Code Online (Sandbox Code Playgroud)
我真的希望有人能够尽可能好地解释这些事情.真的很感激.
好吧,如果有人感兴趣,我有答案。
该代码使用 setOneShotPreviewCallback() 调用 onPreviewFrame() 重复从相机预览中拍摄快照。该帧以 YUV 格式传送,因此 raw2jpg() 将其转换为 32 位 ARGB 以供 jpeg 编码器使用。NV21 是一种 YUV 平面格式,如此处所述。
getPicture() 可能由应用程序调用,并在私有字节数组 mCurrentFrame 中生成图像的 jpeg 数据并返回该数组。如果之后不在该代码片段中会发生什么。请注意,getPicture() 执行了几个 wait()。这是因为图像采集代码在与应用程序不同的线程中运行。
在 Main 活动中,公共静态字节 CurrentJPEG 得到:cameraFrame.getPicture(); 在 public void run() 中。在此 Web 服务中,它通过套接字发送到所需的 IP。
如我错了请纠正我。
现在我仍然想知道图像如何在浏览器中显示为图片,因为您向它发送字节数据,对吗?请查看:http://code.google.com/p/camdroiduni/source/browse/trunk/code/eclipse_workspace/camdroid/src/de/aes/camdroid/WebServer.java