Need to understand some java code
<p>I'm fairly new to android programming, but I am a quick learner.
So I found an intresting piece of code here: <a href="http://code.google.com/p/camdroiduni/source/browse/trunk/code/eclipse_workspace/camdroid/src/de/aes/camdroid/CameraView.java" rel="nofollow">http://code.google.com/p/camdroiduni/source/browse/trunk/code/eclipse_workspace/camdroid/src/de/aes/camdroid/CameraView.java</a></p>

<p>And it's about live streaming from your device's camera to your browser.</p>

<p>But I want to know how the code actually works.</p>

<p>These are the things I want to understand:</p>

<p>1) How do they stream to the webbrowser. I understand that they send a index.html file to the ip adress of the device (on wifi) and that file reloads the page every second. But <strong>how</strong> do they send the index.html file to the <strong><em>desired</em> ip address with sockets?</strong></p>

<p>2) <a href="http://code.google.com/p/camdroiduni/wiki/Status#save_pictures_frequently" rel="nofollow">http://code.google.com/p/camdroiduni/wiki/Status#save_pictures_frequently</a> , Here they mention they are using video, but I am still convinced they take pictures and send them as I don't see the mediarecorder anywhere.</p>

<p>Now my question is how they keep sending AND saving those images into the SD folder (i think). I think it's done with this code, but how does it works. Like with c.takepicture, it takes long to save and start previewing again, so that's no option to livestream.</p>

<pre><code>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&gt;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 &lt; height; j++) {
        int uvp = frameSize + (j &gt;&gt; 1) * width, u = 0, v = 0;
        for (int i = 0; i &lt; width; i++, yp++) {
            int y=0;
            if(yp &lt; raw.length) {
                y = (0xff &amp; ((int) raw[yp])) - 16;
            }
            if (y &lt; 0) y = 0;
            if ((i &amp; 1) == 0) {
                if(uvp&lt;raw.length) {
                    v = (0xff &amp; raw[uvp++]) - 128;
                    u = (0xff &amp; 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 &lt; 0) r = 0; else if (r &gt; 262143) r = 262143;
            if (g &lt; 0) g = 0; else if (g &gt; 262143) g = 262143;
            if (b &lt; 0) b = 0; else if (b &gt; 262143) b = 262143;

            rgb[yp] = 0xff000000 | ((r &lt;&lt; 6) &amp; 
                      0xff0000) | ((g &gt;&gt; 2) &amp;
                      0xff00) | ((b &gt;&gt; 10) &amp; 
                      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();
}
</code></pre>

<p>I really hope someone can explain these things as good as possible. That would really much be appreciated.</p>
