-2

我正在编写一个代码,我试图打开设备的摄像头,但我的命令不起作用,它显示编译时错误,请查看命令正确还是错误,如果错误,请提供我是正确的代码,这是我的代码 -

Camera camera= Camera.open();
4

3 回答 3

2

我想你已经添加了,但仅供参考

 <uses-permission android:name="android.permission.CAMERA" />

请查看链接.......

如果不起作用,请尝试所有这些

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
于 2012-06-14T07:08:52.320 回答
2

尝试使用这个 -

import android.hardware.Camera;
于 2012-06-14T07:24:26.830 回答
1

按照这个它应该工作。

您可以传递一个 Intent 来启动相机并像

private Uri imageUri;

public void takePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }
}
于 2012-06-14T07:09:56.640 回答