当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Having Problems with Camera Preview on Android

问题描述:

I kow this might be an old topic and gone through certain threads on stack overflow, but couldn't get a satisfying anwser, thus asking it again.

I created a camera preview for my application and tested on my phone, it worked well, but i tried on tablet, which either lead to crashing or just a black screen.

already checked the problem about supportedpreviewsize, but still doesn't work, here's the code for

camera preview

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();

}

}

Using camera preview in activity

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;

}*/

}

and 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

网友答案:

in your activity you have initialized your cam before onCereate(). might be the problem

网友答案:

Have you initialized sview? your code:

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

Well found the answer.. I was setting incorrect parameters... here's the working code..

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();


}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: