相机应用程序在 Android 启动时出现异常

发布于 2024-11-27 23:46:29 字数 3884 浏览 0 评论 0原文

为了开始在 Android 中使用相机应用程序,我编写了一些代码来执行以下步骤: 1.预览
2.捕获图像
3.保存图片
4.再次预览

这是执行上述步骤的完整代码:

public class CameraActivity extends Activity implements SurfaceHolder.Callback,Camera.AutoFocusCallback,Camera.PictureCallback
{
String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
private Camera mCamera;
boolean mPreviewRunning = false;
ImageButton takePictureButton;
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.camera);

    mSurfaceView = (SurfaceView)findViewById(R.id.surface);
    takePictureButton = (ImageButton)findViewById(R.id.capture_img);
    takePictureButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mCamera.takePicture(null, null, CameraActivity.this);
        }
    });
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

    Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
    startActivityForResult(takePictureFromCameraIntent, 1);

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    //Picture taken from camera
    if(requestCode == 1)
    {
        if(resultCode == Activity.RESULT_OK)
        {
            File f = new File(SD_CARD_TEMP_DIR);
            try{
                Uri capturedImage = 
                    Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), 
                            null, null));

                Log.i("camera", "Selected image: "+capturedImage.toString());
                f.delete();
            }catch (FileNotFoundException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
    else
    {
        Log.i("camera", "Result Code was: " + resultCode);
    }
}
@Override
public void onAutoFocus(boolean success, Camera camera) {
    // TODO Auto-generated method stub
    camera.takePicture(null, null, this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(mPreviewRunning)
    {
        mCamera.stopPreview();
    }
    Camera.Parameters p = mCamera.getParameters();
    p.setPreviewSize(width, height);
    p.setPictureFormat(PixelFormat.JPEG);
    mCamera.setParameters(p);

    try
    {
        mCamera.setPreviewDisplay(holder);
    }catch(IOException ie)
    {
        ie.printStackTrace();
    }
    mCamera.startPreview();
    mPreviewRunning = true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCamera = Camera.open();

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCamera.stopPreview();
    mPreviewRunning = false;
    mCamera.release();

}
 }

当我运行上述代码时,在应用程序本身启动时,它给我一条消息,表明它已意外停止。 我在这里犯了什么错误吗?我想知道,因为我是相机新手。请帮我!!! 谢谢

To get started with camera application in android I have written some code to perform the following steps:
1.Preview
2.Capture Image
3.Save Image
4.Preview Again

Here is the whole code to perform the above steps:

public class CameraActivity extends Activity implements SurfaceHolder.Callback,Camera.AutoFocusCallback,Camera.PictureCallback
{
String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
private Camera mCamera;
boolean mPreviewRunning = false;
ImageButton takePictureButton;
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.camera);

    mSurfaceView = (SurfaceView)findViewById(R.id.surface);
    takePictureButton = (ImageButton)findViewById(R.id.capture_img);
    takePictureButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mCamera.takePicture(null, null, CameraActivity.this);
        }
    });
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

    Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
    startActivityForResult(takePictureFromCameraIntent, 1);

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    //Picture taken from camera
    if(requestCode == 1)
    {
        if(resultCode == Activity.RESULT_OK)
        {
            File f = new File(SD_CARD_TEMP_DIR);
            try{
                Uri capturedImage = 
                    Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), 
                            null, null));

                Log.i("camera", "Selected image: "+capturedImage.toString());
                f.delete();
            }catch (FileNotFoundException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
    else
    {
        Log.i("camera", "Result Code was: " + resultCode);
    }
}
@Override
public void onAutoFocus(boolean success, Camera camera) {
    // TODO Auto-generated method stub
    camera.takePicture(null, null, this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(mPreviewRunning)
    {
        mCamera.stopPreview();
    }
    Camera.Parameters p = mCamera.getParameters();
    p.setPreviewSize(width, height);
    p.setPictureFormat(PixelFormat.JPEG);
    mCamera.setParameters(p);

    try
    {
        mCamera.setPreviewDisplay(holder);
    }catch(IOException ie)
    {
        ie.printStackTrace();
    }
    mCamera.startPreview();
    mPreviewRunning = true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCamera = Camera.open();

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCamera.stopPreview();
    mPreviewRunning = false;
    mCamera.release();

}
 }

When I run the above code, at the start of the app itself it is giving me a message that it has stopped unexpectedly.
Have I done any mistake here? I want to know because I am new for Camera. Please help me!!!
Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-12-04 23:46:29

您是否在清单中包含了相机权限?

使用权限 android:name="android.permission.CAMERA"

Have you include Camera permission in Manifest?

uses-permission android:name="android.permission.CAMERA"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文