{"id":382,"date":"2011-03-30T20:35:00","date_gmt":"2011-03-30T20:35:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/android-http-camera-live-preview-tutorial.html"},"modified":"2012-10-21T19:39:12","modified_gmt":"2012-10-21T19:39:12","slug":"android-http-camera-live-preview","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html","title":{"rendered":"Android HTTP Camera Live Preview Tutorial"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">The Android SDK includes a <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html\">Camera<\/a> class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding for video. It is a handy abstraction of the real hardware device. However, the SDK doesn&#8217;t provide any camera emulation, something that makes testing camera enabled applications quite difficult. In this tutorial, I am going to show you how to use a web camera in order to obtain a live camera preview. <\/p>\n<div dir=\"ltr\" style=\"text-align: left\">\nWhat we are essentially going to do is setup a web camera to publish static images in a predefined URL and then grab these images from our application and present them as motion pictures. Note that this tutorial was inspired by a post named <a href=\"http:\/\/www.tomgibara.com\/android\/camera-source\">\u201cLive Camera Previews in Android\u201d<\/a>.<\/p>\n<p>Let&#8217;s start by creating an Eclipse project under the name \u201cAndroidHttpCameraProject\u201d and an Activity named \u201cMyCamAppActivity\u201d. As a first step, I am going to show you how to use the SDK camera. You can find many resources on how to manipulate the camera, one of them being the <a href=\"http:\/\/developer.android.com\/resources\/samples\/ApiDemos\/src\/com\/example\/android\/apis\/graphics\/CameraPreview.html\">CameraPreview<\/a> class that is included in the SDK&#8217;s samples. In short, we are going to extend the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceView.html\">SurfaceView<\/a> class and provide our implementation using the device&#8217;s camera. Then, we use the <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#setContentView%28android.view.View%29\">setContentView<\/a> method of our <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html\">Activity<\/a> in order to set the activity content to that specific <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/View.html\">View<\/a>. Let&#8217;s see what the two classes look like:<\/p>\n<p><strong>MyCamAppActivity<\/strong><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.camera;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.view.SurfaceView;\r\nimport android.view.Window;\r\n\r\npublic class MyCamAppActivity extends Activity {\r\n    \r\n    private SurfaceView cameraPreview;\r\n\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        \r\n        super.onCreate(savedInstanceState);\r\n        requestWindowFeature(Window.FEATURE_NO_TITLE);\r\n        \r\n        cameraPreview = new CameraPreview(this);\r\n        \r\n        setContentView(cameraPreview);\r\n        \r\n    }\r\n    \r\n}\r\n<\/pre>\n<p><strong>CameraPreview<\/strong><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.camera;\r\n\r\nimport android.content.Context;\r\nimport android.hardware.Camera;\r\nimport android.util.Log;\r\nimport android.view.SurfaceHolder;\r\nimport android.view.SurfaceView;\r\n\r\npublic class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {\r\n\r\n    private SurfaceHolder holder;\r\n    private Camera camera;\r\n\r\n    public CameraPreview(Context context) {\r\n        super(context);\r\n        holder = getHolder();\r\n        holder.addCallback(this);\r\n        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);\r\n    }\r\n\r\n    @Override\r\n    public void surfaceChanged(SurfaceHolder holder2, int format, int w, int h) {\r\n        Camera.Parameters parameters = camera.getParameters();\r\n        parameters.setPreviewSize(w, h);\r\n        camera.setParameters(parameters);\r\n        camera.startPreview();\r\n    }\r\n\r\n    @Override\r\n    public void surfaceCreated(SurfaceHolder holder1) {\r\n        try {\r\n            camera = Camera.open();\r\n            camera.setPreviewDisplay(holder1);\r\n        } \r\n        catch (Exception e) {\r\n            Log.i(\"Exception surfaceCreated()\", \"e=\" + e);\r\n            camera.release();\r\n            camera = null;\r\n        }\r\n\r\n    }\r\n\r\n    @Override\r\n    public void surfaceDestroyed(SurfaceHolder arg0) {\r\n        camera.stopPreview();\r\n        camera.release();\r\n        camera = null;\r\n    }\r\n    \r\n}\r\n<\/pre>\n<p>In our \u201cCameraPreview\u201d class, we implement the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.Callback.html\">SurfaceHolder.Callback<\/a> interface so that we receive information about changes to the underlying surface. The methods that have to be implemented are the following:<\/p>\n<ul>\n<li><a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.Callback.html#surfaceCreated%28android.view.SurfaceHolder%29\">surfaceCreated<\/a>: Called immediately after the surface is first created. <\/li>\n<li><a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.Callback.html#surfaceChanged%28android.view.SurfaceHolder,%20int,%20int,%20int%29\">surfaceChanged<\/a>: Called immediately after any structural changes (format or size) have been made to the surface.<\/li>\n<li><a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.Callback.html#surfaceDestroyed%28android.view.SurfaceHolder%29\">surfaceDestroyed<\/a>: Called immediately before a surface is being destroyed.<\/li>\n<\/ul>\n<p>To use the <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html\">Camera<\/a> class, no constructor is available, but we rather use the <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html#open%28%29\">open<\/a> method which creates a new <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html\">Camera<\/a> object to access the first back-facing camera on the device. Then, we use the <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html#setPreviewDisplay%28android.view.SurfaceHolder%29\">setPreviewDisplay<\/a> method to set the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/Surface.html\">Surface<\/a> to be used for the live preview. Whenever a change occurs, we call the <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html#startPreview%28%29\">startPreview<\/a> method in order to start capturing and drawing preview frames to the screen, after we have set the appropriate preview size (<a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.Parameters.html#setPreviewSize%28int,%20int%29\">setPreviewSize<\/a>) at the <a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.Parameters.html\">Camera.Parameters<\/a>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>If we now launch the Eclipse configuration, we will come across a totally not-helpful image, as follows:<\/p>\n<p><a href=\"http:\/\/2.bp.blogspot.com\/_piNjpdpJZXA\/TR-2vuKmReI\/AAAAAAAAAQk\/mojpzY9VBSo\/s1600\/01-camera-mock.png\"><img decoding=\"async\" alt=\"\" border=\"0\" src=\"http:\/\/2.bp.blogspot.com\/_piNjpdpJZXA\/TR-2vuKmReI\/AAAAAAAAAQk\/mojpzY9VBSo\/s320\/01-camera-mock.png\" style=\"cursor: pointer;height: 320px;margin: 0px auto 10px;text-align: center;width: 215px\" \/><\/a><\/p>\n<p>Now we are going to replace that surface view with a custom one that use static images from a web camera. First, let&#8217;s see the changes to our activity. The new version is the following:<\/p>\n<p><strong>MyCamAppActivity<\/strong><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.camera;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.view.Display;\r\nimport android.view.SurfaceView;\r\nimport android.view.Window;\r\n\r\npublic class MyCamAppActivity extends Activity {\r\n    \r\n    private int viewWidth;\r\n    private int viewHeight;\r\n    \r\n    private SurfaceView cameraPreview;\r\n    \r\n    private static final boolean useHttpCamera = true;\r\n\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        \r\n        super.onCreate(savedInstanceState);\r\n        requestWindowFeature(Window.FEATURE_NO_TITLE);\r\n        \r\n        calculateDisplayDimensions();\r\n        \r\n        if (useHttpCamera) {\r\n            cameraPreview = new HttpCameraPreview(this, viewWidth, viewHeight);\r\n        }\r\n        else {\r\n            cameraPreview = new CameraPreview(this);\r\n        }\r\n        setContentView(cameraPreview);\r\n        \r\n    }\r\n    \r\n    private void calculateDisplayDimensions() {\r\n        Display display = getWindowManager().getDefaultDisplay();\r\n        viewWidth = display.getWidth();\r\n        viewHeight = display.getHeight();\r\n    }    \r\n    \r\n}\r\n<\/pre>\n<p>We have added a boolean variable to define which version we want to use, the one with the \u201creal\u201d camera or the one that uses the web cam. The latter, uses a new class named \u201cHttpCameraPreview\u201d which also extends the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceView.html\">SurfaceView<\/a> class. This one requires to know the width and the height of the  enclosed view, thus we use the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/WindowManager.html\">WindowManager<\/a> class to get the default <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/Display.html\">Display<\/a> and from that we calculate the dimensions.<\/p>\n<p>Here is what the new class looks like:<\/p>\n<p><strong>HttpCameraPreview<\/strong><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.camera;\r\n\r\nimport android.content.Context;\r\nimport android.graphics.Canvas;\r\nimport android.util.Log;\r\nimport android.view.SurfaceHolder;\r\nimport android.view.SurfaceView;\r\n\r\npublic class HttpCameraPreview extends SurfaceView implements SurfaceHolder.Callback {\r\n    \r\n    private static final String url = \"http:\/\/10.0.2.2:8080\";\r\n    \r\n    private CanvasThread canvasThread;\r\n    \r\n    private SurfaceHolder holder;\r\n    private HttpCamera camera;\r\n    \r\n    private int viewWidth;\r\n    private int viewHeight;\r\n\r\n    public HttpCameraPreview(Context context, int viewWidth, int viewHeight) {\r\n        super(context);\r\n        holder = getHolder();\r\n        holder.addCallback(this);\r\n        holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);\r\n        this.viewWidth = viewWidth;\r\n        this.viewHeight = viewHeight;\r\n        canvasThread = new CanvasThread();\r\n    }\r\n\r\n    @Override\r\n    public void surfaceChanged(SurfaceHolder holder2, int format, int w, int h) {        \r\n        try {            \r\n            Canvas c = holder.lockCanvas(null);\r\n            camera.captureAndDraw(c);\r\n            \r\n            if (c != null) {\r\n                holder.unlockCanvasAndPost(c);\r\n            }            \r\n        } \r\n        catch (Exception e) {\r\n            Log.e(getClass().getSimpleName(), \"Error when surface changed\", e);\r\n            camera = null;\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void surfaceCreated(SurfaceHolder arg0) {\r\n        try {\r\n            camera = new HttpCamera(url, viewWidth, viewHeight, true);\r\n            canvasThread.setRunning(true);\r\n            canvasThread.start();\r\n        } \r\n        catch (Exception e) {\r\n            Log.e(getClass().getSimpleName(), \"Error while creating surface\", e);\r\n            camera = null;\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void surfaceDestroyed(SurfaceHolder arg0) {\r\n        camera = null;\r\n        boolean retry = true;\r\n        canvasThread.setRunning(false);\r\n        while (retry) {\r\n            try {\r\n                canvasThread.join();\r\n                retry = false;\r\n            } catch (InterruptedException e) {\r\n            }\r\n        }\r\n\r\n    }\r\n    \r\n    private class CanvasThread extends Thread {\r\n        \r\n        private boolean running;\r\n        \r\n        public void setRunning(boolean running){\r\n            this.running = running;\r\n        }\r\n        \r\n        public void run() {\r\n            \r\n           while (running) {               \r\n               Canvas c = null;\r\n               try {\r\n                   c = holder.lockCanvas(null);           \r\n                   synchronized (holder) {\r\n                       camera.captureAndDraw(c);\r\n                   }\r\n               }\r\n               catch (Exception e) {\r\n                   Log.e(getClass().getSimpleName(), \"Error while drawing canvas\", e);\r\n               }\r\n               finally {\r\n                   if (c != null) {\r\n                       holder.unlockCanvasAndPost(c);\r\n                   }\r\n               }\r\n           }\r\n        }\r\n        \r\n    }\r\n\r\n}\r\n<\/pre>\n<p>In the constructor, we start a thread that will be responsible for updating the <a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/Canvas.html\">Canvas<\/a> of the surface view, which we will describe in a while. We implement again the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.Callback.html\">SurfaceHolder.Callback<\/a> interface and the three methods describer above. Note that a custom \u201cHttpCamera\u201d object is used, which captures the still images and draws them to the <a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/Canvas.html\">Canvas<\/a>. The <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.html#lockCanvas%28%29\">lockCanvas<\/a> method of the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.html\">SurfaceHolder<\/a> is used in order draw into the surface&#8217;s bitmap and the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceHolder.html#unlockCanvasAndPost%28android.graphics.Canvas%29\">unlockCanvasAndPost<\/a> method is invoked after the drawing has completed. The \u201cHttpCamera\u201d object uses a URL which is where the images get published. Note that the IP address is the 10.0.2.2, which actually is the machine hosting  the Android emulator. We can not use the classic localhost address (127.0.0.1), since this points to the emulator itself. In the thread class, we have a loop in which we continuously draw on the underlying canvas. We use a boolean variable as a flag to indicate whether the process should continue or not.<\/p>\n<p>Let&#8217;s now see what this \u201cHttpCamera\u201d class is all about:<\/p>\n<p><strong>HttpCamera<\/strong><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.camera;\r\n\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.net.HttpURLConnection;\r\nimport java.net.URL;\r\n\r\nimport android.graphics.Bitmap;\r\nimport android.graphics.BitmapFactory;\r\nimport android.graphics.Canvas;\r\nimport android.graphics.Paint;\r\nimport android.graphics.Rect;\r\n\r\npublic class HttpCamera {\r\n    \r\n    private static final int CONNECT_TIMEOUT = 1000;\r\n    private static final int SOCKET_TIMEOUT = 1000;\r\n    \r\n    private final String url;\r\n    private final Rect bounds;\r\n    private final boolean preserveAspectRatio;\r\n    private final Paint paint = new Paint();\r\n\r\n    public HttpCamera(String url, int width, int height, boolean preserveAspectRatio) {\r\n        this.url = url;\r\n        bounds = new Rect(0, 0, width, height);\r\n        this.preserveAspectRatio = preserveAspectRatio;\r\n        \r\n        paint.setFilterBitmap(true);\r\n        paint.setAntiAlias(true);\r\n    }\r\n\r\n    private Bitmap retrieveBitmap() throws IOException {\r\n        \r\n        Bitmap bitmap = null;\r\n        InputStream in = null;\r\n        int response = -1;\r\n\r\n        try {\r\n            \r\n            URL url = new URL(this.url);        \r\n            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();\r\n            \r\n            httpConn.setConnectTimeout(CONNECT_TIMEOUT);\r\n            httpConn.setReadTimeout(SOCKET_TIMEOUT);\r\n            httpConn.setRequestMethod(\"GET\");\r\n            \r\n            httpConn.connect();\r\n            response = httpConn.getResponseCode();\r\n            \r\n            if (response == HttpURLConnection.HTTP_OK) {\r\n                in = httpConn.getInputStream();\r\n                bitmap = BitmapFactory.decodeStream(in);\r\n            }\r\n            \r\n            return bitmap;\r\n            \r\n        } \r\n        catch (Exception e) {\r\n            return null;\r\n        }\r\n        finally {\r\n            if (in != null) try {\r\n                in.close();\r\n            } catch (IOException e) {\r\n                \/* ignore *\/\r\n            }\r\n        }\r\n        \r\n    }\r\n\r\n    public boolean captureAndDraw(Canvas canvas) throws IOException {\r\n        \r\n        Bitmap bitmap = retrieveBitmap();\r\n        \r\n        if (bitmap == null) throw new IOException(\"Response Code: \");\r\n\r\n        \/\/render it to canvas, scaling if necessary\r\n        if (bounds.right == bitmap.getWidth() &amp;&amp; bounds.bottom == bitmap.getHeight()) {\r\n            canvas.drawBitmap(bitmap, 0, 0, null);\r\n        } else {\r\n            Rect dest;\r\n            if (preserveAspectRatio) {\r\n                dest = new Rect(bounds);\r\n                dest.bottom = bitmap.getHeight() * bounds.right \/ bitmap.getWidth();\r\n                dest.offset(0, (bounds.bottom - dest.bottom)\/2);\r\n            } \r\n            else {\r\n                dest = bounds;\r\n            }\r\n            canvas.drawBitmap(bitmap, null, dest, paint);\r\n        }\r\n        \r\n        return true;\r\n        \r\n    }\r\n\r\n}\r\n<\/pre>\n<p>In the constructor, we pass the URL to where the images from the camera should be found. This could be any web camera HTTP server. Additionally, we pass the view&#8217;s dimensions and whether the aspect ratio should be preserved (this should be true in most cases). In the \u201ccaptureAndDraw\u201d method, which is the public method invoked by the custom surface class, we perform HTTP GET requests to the provided URL and retrieve the images as <a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/Bitmap.html\">Bitmap<\/a> objects. We then adjust the dimensions of the image accordingly and draw it on the <a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/Canvas.html\">Canvas<\/a> using the <a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/Canvas.html#drawBitmap%28android.graphics.Bitmap,%20android.graphics.Rect,%20android.graphics.Rect,%20android.graphics.Paint%29\">drawBitmap<\/a> method.<\/p>\n<p>Let&#8217;s now see the manifest file:<\/p>\n<p><strong>AndroidManifest<\/strong><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n      package=\"com.javacodegeeks.android.camera\"\r\n      android:versionCode=\"1\"\r\n      android:versionName=\"1.0\"&gt;\r\n      \r\n    &lt;application android:icon=\"@drawable\/icon\" android:label=\"@string\/app_name\"&gt;\r\n        &lt;activity android:name=\".MyCamAppActivity\"\r\n                  android:label=\"@string\/app_name\"&gt;\r\n            &lt;intent-filter&gt;\r\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n            &lt;\/intent-filter&gt;\r\n        &lt;\/activity&gt;   \r\n    &lt;\/application&gt;\r\n    \r\n    &lt;uses-sdk android:minSdkVersion=\"3\" \/&gt;\r\n    \r\n    &lt;uses-permission android:name=\"android.permission.CAMERA\"\/&gt;\r\n    &lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;\r\n\r\n&lt;\/manifest&gt; \r\n<\/pre>\n<p>The usual stuff here, just remember to add permissions for using the camera (<a href=\"http:\/\/developer.android.com\/reference\/android\/Manifest.permission.html#CAMERA\">android.permission.CAMERA<\/a>) and creating internet connections (<a href=\"http:\/\/developer.android.com\/reference\/android\/Manifest.permission.html#INTERNET\">android.permission.INTERNET<\/a>).<\/p>\n<p>Before we test the application, we need to have images published on a web server. Some web cameras already provide that functionality, but in case yours does not, you can accomplish that using a pretty cool application named <a href=\"http:\/\/www.webcam2000.info\/download.html\">WebCam2000<\/a>. Unfortunately, this is only for Windows machines. After you execute the program, it should automatically find your camera. If not, play a little with the options and make sure that in the \u201cVideo\u201d menu, the \u201cMicrosoft WDM Image Capture\u201d option is enabled. Then, make sure the \u201cEnable Web Server\u201d box is ticked and check that the server is working by pointing your browser to the corresponding URL:<\/p>\n<p><a href=\"http:\/\/localhost:8080\/\">http:\/\/localhost:8080\/<\/a><\/p>\n<p>If you now launch the Eclipse configuration, you will get a camera input to your emulator with the images periodically updated. <\/p>\n<p>You can use this approach in order to provide a \u201cmock camera\u201d to your application and make it easier to test it. The Eclipse project created for this tutorial can be found <a href=\"http:\/\/dl.dropbox.com\/u\/7215751\/JavaCodeGeeks\/AndroidHTTPCameraPreviewTutorial\/AndroidHttpCameraProject.zip\">here<\/a>. <\/p>\n<p>Enjoy and don&#8217;t forget to share!<\/p>\n<div style=\"margin: 0px\"><strong><i>Related Articles :<\/i><\/strong><\/div>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/10\/android-full-application-tutorial.html\">\u201cAndroid Full Application Tutorial\u201d series<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/android-location-based-services.html\">Android Location Based Services Application \u2013 GPS location<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/android-reverse-geocoding-yahoo-api.html\">Android Reverse Geocoding with Yahoo API &#8211; PlaceFinder<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/11\/boost-android-xml-parsing-xml-pull.html\">Boost your Android XML parsing with XML Pull<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/09\/android-text-to-speech-application.html\">Android Text-To-Speech Application<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/06\/install-android-os-on-pc-with.html\">Install Android OS on your PC with VirtualBox<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Android SDK includes a Camera class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding for video. It is a handy abstraction of the real hardware device. However, the SDK doesn&#8217;t provide any camera emulation, something that makes testing camera enabled applications quite difficult. In this &hellip;<\/p>\n","protected":false},"author":3,"featured_media":46,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[160,83],"class_list":["post-382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-core","tag-android-camera","tag-android-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Android HTTP Camera Live Preview Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"The Android SDK includes a Camera class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android HTTP Camera Live Preview Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"The Android SDK includes a Camera class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-03-30T20:35:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T19:39:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ilias Tsagklis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ilias Tsagklis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html\"},\"author\":{\"name\":\"Ilias Tsagklis\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9a83496b285d30c61e8a674625c1350e\"},\"headline\":\"Android HTTP Camera Live Preview Tutorial\",\"datePublished\":\"2011-03-30T20:35:00+00:00\",\"dateModified\":\"2012-10-21T19:39:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html\"},\"wordCount\":1039,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"keywords\":[\"Android Camera\",\"Android Tutorial\"],\"articleSection\":[\"Android Core\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html\",\"name\":\"Android HTTP Camera Live Preview Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"datePublished\":\"2011-03-30T20:35:00+00:00\",\"dateModified\":\"2012-10-21T19:39:12+00:00\",\"description\":\"The Android SDK includes a Camera class that is used to set image capture settings, start\\\/stop preview, snap pictures, and retrieve frames for encoding\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/03\\\/android-http-camera-live-preview.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/android\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Android Core\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/android\\\/android-core\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Android HTTP Camera Live Preview Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9a83496b285d30c61e8a674625c1350e\",\"name\":\"Ilias Tsagklis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g\",\"caption\":\"Ilias Tsagklis\"},\"description\":\"Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.\",\"sameAs\":[\"http:\\\/\\\/www.iliastsagklis.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/iliastsagklis\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/ilias-tsagklis\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android HTTP Camera Live Preview Tutorial - Java Code Geeks","description":"The Android SDK includes a Camera class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html","og_locale":"en_US","og_type":"article","og_title":"Android HTTP Camera Live Preview Tutorial - Java Code Geeks","og_description":"The Android SDK includes a Camera class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding","og_url":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-03-30T20:35:00+00:00","article_modified_time":"2012-10-21T19:39:12+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","type":"image\/jpeg"}],"author":"Ilias Tsagklis","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Ilias Tsagklis","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html"},"author":{"name":"Ilias Tsagklis","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9a83496b285d30c61e8a674625c1350e"},"headline":"Android HTTP Camera Live Preview Tutorial","datePublished":"2011-03-30T20:35:00+00:00","dateModified":"2012-10-21T19:39:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html"},"wordCount":1039,"commentCount":6,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","keywords":["Android Camera","Android Tutorial"],"articleSection":["Android Core"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html","url":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html","name":"Android HTTP Camera Live Preview Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","datePublished":"2011-03-30T20:35:00+00:00","dateModified":"2012-10-21T19:39:12+00:00","description":"The Android SDK includes a Camera class that is used to set image capture settings, start\/stop preview, snap pictures, and retrieve frames for encoding","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/03\/android-http-camera-live-preview.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/www.javacodegeeks.com\/category\/android"},{"@type":"ListItem","position":3,"name":"Android Core","item":"https:\/\/www.javacodegeeks.com\/category\/android\/android-core"},{"@type":"ListItem","position":4,"name":"Android HTTP Camera Live Preview Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9a83496b285d30c61e8a674625c1350e","name":"Ilias Tsagklis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g","caption":"Ilias Tsagklis"},"description":"Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.","sameAs":["http:\/\/www.iliastsagklis.com\/","https:\/\/www.linkedin.com\/in\/iliastsagklis"],"url":"https:\/\/www.javacodegeeks.com\/author\/ilias-tsagklis"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/46"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}