Galaxy平板电脑上的应用程序崩溃



我有一个android项目,它在我的DroidX上运行良好,但在Galaxy平板电脑上不起作用。我收到一条日志消息,说不支持1200x728型相机。我没有在任何地方设置相机分辨率,我相信它只是从设备中获取默认设置。为什么当我尝试在Galaxy Tab上加载它时它会崩溃?

从我的主要活动中,我加载我的相机类,如下所示:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // requesting to turn the title OFF
    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //Set Screen Orientation
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    try{
        //Create Intance of Camera
        camPreview = new CamLayer(this.getApplicationContext());
        //Create Instance of OpenGL
        glView = new GLLayer(this);
        //FrameLayOut for holding everything
        FrameLayout frame = new FrameLayout(this);
        // set as main view
        setContentView(frame);
        // add Camera to view 
        frame.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        frame.addView(glView);

    } catch(Exception e){}
}

然后我的相机类看起来是这样的:

    package com.eliddell.AR;
    import android.content.Context;
    import android.graphics.PixelFormat;
    import android.hardware.Camera;
    import android.hardware.Camera.Parameters;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    public class CamLayer extends SurfaceView {
            Camera camera;
            SurfaceHolder previewHolder;
    public CamLayer(Context context)
    {
        super(context);
        previewHolder = this.getHolder();
        previewHolder.setType
        (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        previewHolder.addCallback(surfaceHolderListener);
    }
    SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
            camera=Camera.open();
        try {
            camera.setPreviewDisplay(previewHolder);
        }
            catch (Throwable e){ }
        }
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
        {
            Parameters params = camera.getParameters();
            params.setPreviewSize(width, height);
            params.setPictureFormat(PixelFormat.JPEG);
            camera.setParameters(params);
            camera.startPreview();
        }
        public void surfaceDestroyed(SurfaceHolder arg0)
        {
            camera.stopPreview();
            camera.release();
        }
    };
    public void onResume() {
        camera.startPreview();
    }
    public void onPause() {
        // TODO Auto-generated method stub
        camera.stopPreview();
    }

}

我认为设置预览参数应该有一些问题。请在下面尝试。

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (isPreviewRunning) {
camera.stopPreview();
}
try{
Camera.Parameters p = camera.getParameters();
if(p!=null){
List<Size> sizes = p.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);  
p.setPreviewSize(optimalSize.width, optimalSize.height);
camera.setParameters(p);
camera.setPreviewDisplay(holder);;
camera.startPreview();
}
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}
isPreviewRunning = true;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
// TODO Auto-generated method stub
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;

int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}

// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (isPreviewRunning) {
camera.stopPreview();
}
try{
Camera.Parameters p = camera.getParameters();
if(p!=null){
List<Size> sizes = p.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
p.setPreviewSize(optimalSize.width, optimalSize.height);
camera.setParameters(p);
camera.setPreviewDisplay(holder);;
camera.startPreview();
}
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}
isPreviewRunning = true;
}
private Size getOptimalPreviewSize(List sizes, int w, int h) {
// TODO Auto-generated method stub
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}

把这个代码放在你的surfaceChanged()中。getOptimalPreviewSize()用于根据设备分辨率设置预览参数。

相关内容

  • 没有找到相关文章

最新更新