I'm having some issues with the preview of the camera on my Android.
Right now i have a button you need to press before you get any preview of the camera. but id like to have a preview as soon as you start the app.
If i try to take the part that starts the preview out of the button and put it into the onCreate, it wont work, the preview wont start.
How can i create a preview without user having to touch a button?
Also, i have a HTC Desire HD, and the preview is turn 90 degrees for me. Why is this happening?
Camera cam;
SurfaceView surf;
SurfaceHolder holder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStart= (Button)findViewById(R.id.startCamPrev);
getWindow().setFormat(PixelFormat.UNKNOWN);
surf = (SurfaceView)findViewById(R.id.surfaceView);
holder = surf.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btnStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
cam = Camera.open();
if(cam != null)
{
try
{
cam.setPreviewDisplay(holder);
cam.startPreview();
}
catch(IOException e)
{
}
}
}
});
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="LSAB.CamTest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CameraTest"
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, CameraTest"
/>
<Button
android:id = "@+id/startCamPrev"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start Preview -"
/>
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
In addition to adding the setPreviewDisplay to the onCreate try doing ->
Implement the SurfaceHolder.Callback
In onSurfaceChanged method reset the setPreviewDisplay with the new surfaceHolder that is passed to that method.
This works for me..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With