我正在开发自己的自定义相机应用程序,运行应用程序时,如果我单击“捕获”按钮,相机将捕获当前位置。实际上,我需要的是在需要时重新拍摄照片。但如果我单击“重新拍摄(拍照)”按钮,应用程序将崩溃。这里我附上了下面的代码。因此,任何人都请帮助我解决错误。
main活动.java
package com.example.camera1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
private Button takePhotoButton;
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}
private PictureCallback capturedIt = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
if(bitmap==null){
Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();
}
cameraObject.release();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhotoButton = (Button)findViewById(R.id.button_takephoto);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
takePhotoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cameraObject = isCameraAvailiable();
cameraObject.stopPreview();
showCamera = new ShowCamera(getApplicationContext(), cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
});
}
public void snapIt(View view){
cameraObject.takePicture(null, null, capturedIt);
}
@Override
public void onPause(){
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
showcamera.java网站
package com.example.camera1;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setDisplayOrientation(90);
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
theCamera.stopPreview();
}
}
活动\主.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.30"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="350dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="snapIt"
android:text="@string/Capture" />
<Button
android:id="@+id/button_takephoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_capture"
android:text="Take Photo" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
最佳答案
我想原因是你在cameraObject.release();
中调用了onPictureTaken
。你可以移除它。别忘了再打电话给startPreview()
。
因为:
此方法仅在预览处于活动状态时(在
startPreview())。拍摄图像后,预览将停止;
调用方若要重新启动,必须再次调用startPreview()
预览或拍摄更多照片。这不应该在
开始()和停止()。
调用此方法后,不能调用startPreview()或take
返回JPEG回调之前的另一张图片。