public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
private Bitmap bitmap;
TextView colorRGB;
Config config;
String callname;
String callnameRimage;
EditText txtData, PatientInfo,PatientAge;
ImageView imageView = null;
ImageView imageView2 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);
Button buttonCapturePreview = (Button) findViewById(R.id.capturepreview);
Button testButton = (Button) findViewById(R.id.testButton);
Button flipButton = (Button) findViewById(R.id.flip);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
Log.d("Log", "onPictureTaken - raw");
}
};
shutterCallback = new ShutterCallback()
{
public void onShutter() {
Log.i("Log", "onShutter'd");
}
};
jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Image");
imagesFolder.mkdirs();
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
name = s.toString() + ".jpg";
File output = new File(imagesFolder, name);
callname = "/sdcard/Asw/Raw Image/" + name;
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{}
Log.d("Log", "onPictureTaken - jpeg");
buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
{
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onClick(View v)
{
if(!previewing)
{
camera = Camera.open(0);
if (camera != null)
{
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}
catch (IOException e)
{
e.printStackTrace();
}
}else txtData.setText("null");
}
}});
buttonCapturePreview.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
captureImage();
}
});
buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
if(camera != null && previewing)
{
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}});
}
private void captureImage()
{
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
private void displayimages()
{
setContentView(R.layout.display);
callnameRimage = "/sdcard/Asw/Raw Image/" + name;
Bitmap bitmap1 = BitmapFactory.decodeFile(callnameRimage);
imageView2=(ImageView)findViewById(R.id.imageView2);
imageView2.setImageBitmap(bitmap1);
Button backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
Intent intent = new Intent(AndroidCamera.this, AndroidCamera.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_camera, menu);
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
camera.stopPreview();
camera.release();
}
}
该计划运行良好。但是当我从应用程序退出时,它会崩溃。我已经添加了logcat错误。我认为有些不对劲的东西在表面被摧毁了。但我无法识别它。任何人都可以建议一种方法来解决它。
12-16 12:24:54.422: E/AndroidRuntime(3806): FATAL EXCEPTION: main
12-16 12:24:54.422: E/AndroidRuntime(3806): java.lang.NullPointerException
12-16 12:24:54.422: E/AndroidRuntime(3806): at com.example.imaging.AndroidCamera.surfaceDestroyed(AndroidCamera.java:512)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.SurfaceView.updateWindow(SurfaceView.java:1099)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:244)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.View.dispatchWindowVisibilityChanged(View.java:5843)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:990)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2467)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.os.Looper.loop(Looper.java:137)
12-16 12:24:54.422: E/AndroidRuntime(3806): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-16 12:24:54.422: E/AndroidRuntime(3806): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 12:24:54.422: E/AndroidRuntime(3806): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 12:24:54.422: E/AndroidRuntime(3806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-16 12:24:54.422: E/AndroidRuntime(3806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-16 12:24:54.422: E/AndroidRuntime(3806): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的LogCat清楚地显示问题所在 - 文件的第512行,方法surfaceDestroyed
。因此,如何释放相机存在一些问题 - 然后到文档!
从这里采取:http://developer.android.com/reference/android/hardware/Camera.html
Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).
所以现在我们需要知道在onPause时引用surfaceDestroyed的时间 - 请参阅此处接受的答案:When does surfaceDestroyed() occur
解决方案:将代码移至onPause。
答案 1 :(得分:1)
试试这个..
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
if(camera != null){
camera.stopPreview();
camera.release();
}
}