views:

181

answers:

2

Hi, i'm developing an app for Android 1.6 on an Zii EGG Handheld. I need to get the preview image of the camera. So i tried to get the image with the PreviewCallback function, but the onPreviewFrame() method is never called. I can only see the preview image on the surface. But my code seems to be right accroding to the documentation and to sample code i found in the internet.

I'll post my Code at the end of this. Maybe someone knows what i've done wrong or maybe knows that this doesen't work with android and the Zii EGG an has an solution.

Regards, Baweg

My code:

CamPreview.java:

package com.test.camtest;

import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CamPreview extends SurfaceView implements SurfaceHolder.Callback {
 Camera mCamera;
 SurfaceHolder mHolder;

 public CamPreview(final Context context, final AttributeSet attrs) {
  super(context, attrs);
  Log.d("METHODCALL", "CamPreview");
  // Install a SurfaceHolder.Callback so we get notified when the
  // underlying surface is created and destroyed.
  this.mHolder = this.getHolder();
  this.mHolder.addCallback(this);
  this.mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 public void surfaceCreated(final SurfaceHolder holder) {
  Log.d("METHODCALL", "surfaceCreated");
  // The Surface has been created, acquire the camera and tell it where
  // to draw.
  this.mCamera = Camera.open();
  try {
   this.mCamera.setPreviewDisplay(holder);

   this.mCamera.setPreviewCallback(new PreviewCallback() {

    public void onPreviewFrame(final byte[] data, final Camera arg1) {
     Log.d("METHODCALL", "onPreviewFrame");
    }
   });
  } catch (final IOException e) {
   e.printStackTrace();
  }
 }

 public void surfaceDestroyed(final SurfaceHolder holder) {
  Log.d("METHODCALL", "surfaceDestroyed");
  // Surface will be destroyed when we return, so stop the preview.
  // Because the CameraDevice object is not a shared resource, it's very
  // important to release it when the activity is paused.
  this.mCamera.stopPreview();
  this.mCamera = null;
 }

 public void surfaceChanged(final SurfaceHolder holder, final int format,
   final int w, final int h) {
  Log.d("METHODCALL", "surfaceChanged");
  // Now that the size is known, set up the camera parameters and begin
  // the preview.
  // final Camera.Parameters parameters = this.mCamera.getParameters();
  // parameters.setPreviewSize(w, h);
  // this.mCamera.setParameters(parameters);
  this.mCamera.startPreview();
 }

 @Override
 public void onDraw(final Canvas canvas) {
  Log.d("METHODCALL", "onDraw");
 }
}

CamTest.java:

package com.test.camtest;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class CamTest extends Activity {
 CamPreview preview;
 boolean initialized = false;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(final Bundle savedInstanceState) {
  Log.d("METHODCALL", "onCreate");
  super.onCreate(savedInstanceState);

  if (!this.initialized) {
   this.setFullScreen();

   this.setContentView(R.layout.main);

   this
     .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

   this.preview = (CamPreview) this.findViewById(R.id.camPreview);
   this.initialized = true;
  }
 }

 private void setFullScreen() {
  Log.d("METHODCALL", "setFullScreen");
  this.getWindow().setFormat(PixelFormat.TRANSLUCENT);

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);

  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

  WindowManager.LayoutParams.FLAG_FULLSCREEN);
 }
}
A: 

Could you provide the source codes of "main.xml" ?

HYKwok
A: 

main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout">
        <de.fkie.camstreamer.gui.CamStreamerFrame
            android:id="@+id/camPreview" android:layout_width="360px"
            android:layout_height="270px" android:layout_centerHorizontal="true"/>
    </RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0" package="de.fkie.camstreamer.main">
          <uses-permission android:name="android.permission.CAMERA"/>
          <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
          <uses-permission android:name="android.permission.RECORD_AUDIO"/>
          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
          <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
          <uses-permission android:name="android.permission.INTERNET"/>
          <uses-sdk android:minSdkVersion="4" 
                  android:targetSdkVersion="4"
                  android:maxSdkVersion="4" />
        <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" >
            <activity android:name=".CamStreamer"
                      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>
    </manifest>