Remember to grant permission to access Camera AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidCamera"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidCamera"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="@+id/camerapreview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
AndroidCamera.java
package com.exercise.AndroidCamera;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
Download the files.
next:
- Add a overlay on Camera Preview SurfaceView
This comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteI am new to Android, is it possible to keep the surfaceView to be portrait? I have other components to display,when I set setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); the entire screen become landscape, which is not what I intended, yet if I remove this line the camera View is rotated by 90 degree to the right by default. How should it be resolved?
Is it http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)?
ReplyDeleteActually, I haven't tried it! Sorry.
Hi there,
ReplyDeleteI tried this tutorial, but eclipse is complaining some of the methods are deprecated, could you please update this tutorial to 2012?
Thanks :)
Hi
ReplyDeleteHow i can remove this vertical line.
hello Yogesh tatwal,
ReplyDeletewhat vertical line?
if i want to use front camera how i must to change this code please tell me thank you newwebsite01@gmail.com
ReplyDeleteHello, thanks for the example. It's running on my nexus 4 but it seems to run slow, roughly 15fps. Any ideas how to speed it up?
ReplyDeleteI tried ur code above.....but it is showing the following error
ReplyDeletejava.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.(Camera.java:500)
at android.hardware.Camera.open(Camera.java:467)
at tk.bingoapp.trialbingo.AndroidCamera.surfaceCreated(AndroidCamera.java:60)
at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
at android.view.SurfaceView.access$000(SurfaceView.java:90)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:195)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2191)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at
dalvik.system.NativeStart.main(Native Method)
Application closes when it is launched
pls help me solving this.