我在我的

Android应用程序上尝试相机预览.当我试用我的真实设备时,它会给我黑屏.

这是我的代码,它不会抛出任何错误,但屏幕仍然是黑色的.有任何想法吗?

import android.app.Activity;

import android.content.Context;

import android.graphics.PixelFormat;

import android.hardware.Camera;

import android.hardware.Camera.Parameters;

import android.os.Bundle;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.widget.FrameLayout;

public class ARrazerNav extends Activity{

public void onCreate(Bundle savedInstanceState){

try{

super.onCreate(savedInstanceState);

CustomCameraView cv = new CustomCameraView(this.getApplicationContext());

FrameLayout r1 = new FrameLayout(this.getApplicationContext());

setContentView(r1);

r1.addView(r1);

}catch (Exception e) {

// TODO: handle exception

}

}

public class CustomCameraView extends SurfaceView{

Camera camera;

SurfaceHolder previewHolder;

public CustomCameraView(Context ctx){

super(ctx);

previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

previewHolder.addCallback(surfaceHolderListener);

}

SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback(){

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

camera.stopPreview();

camera.release();

}

public void surfaceCreated(SurfaceHolder holder) {

// TODO Auto-generated method stub

camera = Camera.open();

try{

camera.setPreviewDisplay(previewHolder);

}catch (Throwable th) {

// TODO: handle exception

}

}

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

Parameters params = camera.getParameters();

params.setPreviewSize(width, height);

params.setPictureFormat(PixelFormat.JPEG);

camera.setParameters(params);

camera.startPreview();

}

};

}

}

有人可以给我一些解释为什么它给我黑屏?

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐