在Android上遇到摄像头预览问题

时间:2013-10-31 20:01:05

标签: java android eclipse camera android-camera

我知道这可能是一个古老的主题并且在堆栈溢出时经历了某些线程,但是无法获得令人满意的anwser,因此再次询问它。

我为我的应用程序创建了一个摄像头预览并在我的手机上进行了测试,它运行良好,但我尝试使用平板电脑,这会导致崩溃或只是黑屏。

已经检查了有关supportedpreviewsize的问题,但仍然无效,这里是代码 相机预览

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Bettercam extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder sholder = null;
SurfaceView sview = null;
Camera cam = null;

public Bettercam(Context context, Camera camera) 
{
    super(context);
    sholder = sview.getHolder();
    sholder.addCallback(this);
}


//method which gets the best  size for displaying preview
public Size getBstSize(int width, int height,Camera.Parameters param)

{
    Size result= null;
    for(Size size : param.getSupportedPreviewSizes())
    {
        if(size.width <= width && size.height <= height)
        {
            if(result==null)
            {
                result=size;
            }
        }
        else
        {
            int newarea = size.width  * size.height;
            int resultarea = result.width * result.height;
            if(newarea > resultarea)
            {
                result=size;
            }
        }
    }

    return (result);

}


//Preview Intializing Method
public void Initpreview(int width, int height)
{
        cam = Camera.open();
        Camera.Parameters parameters=cam.getParameters();
        Camera.Size size=getBstSize(width, height,parameters);
        parameters.setPreviewSize(size.width, size.height);
          cam.setParameters(parameters);
        try
        {
            cam.setPreviewDisplay(sholder);
        }
        catch (Throwable ex)
        {
            cam.release();
            Log.e("Exception in setpreviewdisplay", ex.toString());

        }


}

public void startprev()
{

        cam.startPreview();

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
{
    Initpreview(width, height);

    startprev();


}

@Override
public void surfaceCreated(SurfaceHolder holder) 
{
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) 
{
    cam.stopPreview();
    cam.setPreviewCallback(null);
    cam.release();


}

}

在活动中使用相机预览

public class Glass extends Activity 
{
static Context cont;
//  Camera cam = accesscam();
 Camera cam = Camera.open();    
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_glass);


    try
    {
        Bettercam campre = new Bettercam(this, cam);
        FrameLayout frm = (FrameLayout) findViewById(R.id.frameglass);
        frm.addView(campre);
    }
    catch(Exception ex)
    {



    }
}
/*static Camera accesscam()
{
    Camera c = null;
            try
            {
                c = Camera.open();
            }
            catch (Exception ex)
            {
                Log.d("Unable to open camera", ex.getMessage());
            }


        return c;


}*/
}

和Logcat ...

10-27 17:58:26.640: W/dalvikvm(7380): threadid=1: thread exiting with uncaught       exception (group=0x412b02a0)
10-27 17:58:26.648: E/AndroidRuntime(7380): FATAL EXCEPTION: main
10-27 17:58:26.648: E/AndroidRuntime(7380): java.lang.RuntimeException: Unable to start    activity ComponentInfo{test.argsimulation/test.argsimulation.Glass}: java.lang.RuntimeException: setView must have been called
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.ActivityThread.access$700(ActivityThread.java:146)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.os.Looper.loop(Looper.java:137)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.ActivityThread.main(ActivityThread.java:4949)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at java.lang.reflect.Method.invoke(Method.java:511)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at dalvik.system.NativeStart.main(Native Method)
10-27 17:58:26.648: E/AndroidRuntime(7380): Caused by: java.lang.RuntimeException: setView must have been called
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.widget.Toast.show(Toast.java:103)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at test.argsimulation.Glass.onCreate(Glass.java:28)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.Activity.performCreate(Activity.java:5185)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-27 17:58:26.648: E/AndroidRuntime(7380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
10-27 17:58:26.648: E/AndroidRuntime(7380):     ... 11 more

3 个答案:

答案 0 :(得分:0)

在你的活动中,你在onCereate()之前初始化了你的摄像头。可能是问题

答案 1 :(得分:0)

您是否已初始化sview?你的代码:

SurfaceView sview = null;
public Bettercam(Context context, Camera camera) 
{
    super(context);
    sholder = sview.getHolder(); // sview = ?
    sholder.addCallback(this);
}

答案 2 :(得分:0)

很好找到答案.. 我设置了错误的参数...... 这是工作代码..

public class Bettercam extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder sholder;
Camera cam;

public Bettercam(Context context, Camera camera) 
{
    super(context);
    cam = camera;
    sholder = getHolder();
    sholder.addCallback(this);
}


//method which gets the best  size for displaying preview
/*public Size getBstSize(int width, int height,Camera.Parameters param)

{
    Size result= null;
    for(Size size : param.getSupportedPreviewSizes())
    {
        if(size.width <= width && size.height <= height)
        {
            if(result==null)
            {
                result=size;
            }
        }
        else
        {
            int newarea = size.width  * size.height;
            int resultarea = result.width * result.height;
            if(newarea > resultarea)
            {
                result=size;
            }
        }
    }

    return (result);

}
*/

//Preview Intializing Method
public void Initpreview(int width, int height)
{
        /*Camera.Parameters parameters=cam.getParameters();
        Camera.Size size=getBstSize(width, height,parameters);
        parameters.setPreviewSize(size.width, size.height);
          cam.setParameters(parameters);*/
        try
        {
            cam.setPreviewDisplay(sholder);
            cam.startPreview();
        }
        catch (Throwable ex)
        {
            cam.stopPreview();
            cam.release();
            Log.e("Exception in setpreviewdisplay", ex.toString());

        }


}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
{

    if(sholder.getSurface()==null)
    {
        return;
    }


}

@Override
public void surfaceCreated(SurfaceHolder holder) 
{
    try
    {
        cam.setPreviewDisplay(sholder);
        cam.startPreview();
    }
    catch (Throwable ex)
    {
        cam.stopPreview();
        cam.release();
        Log.e("Exception in setpreviewdisplay", ex.toString());

    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) 
{
    cam.stopPreview();
    cam.setPreviewCallback(null);
    cam.release();


}

}