Android 简单相机活动

发布于 2024-11-28 22:49:59 字数 2698 浏览 0 评论 0原文

我只是想让固定电话自动化拍照。 (我不想在 UI 中预览,只需要将图像保存到 SD 卡)。我在调用 camera.open() 的行收到“无法连接到相机服务”的消息。大多数示例都使用表面实现回调,但这是不必要的。我做错了什么?我的清单中有正确的权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>

这是我正在使用的类,但显然有问题!

public class SimpleCameraActivity extends Activity {

    Context mContext = this;

    Button takePicture;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.setContentView(R.layout.main);

        takePicture = (Button) findViewById(R.id.takePicture);

        takePicture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                takePicture();
            }
        });

    }

    public void takePicture() {
        Camera camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        parameters.set("camera-id", 2);
        camera.setParameters(parameters);
        camera.takePicture(null, rawCallback, null);
        camera.release();
    }

    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera camera) {
            if (imageData != null) {
                saveImage(mContext, imageData, 100);
            }
        }
    };

    public void saveImage(Context mContext, byte[] imageData, int quality) {
        FileOutputStream fileOutputStream = null;

        String filePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        filePath += "/image.jpg";

        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 5;

            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length, options);

            fileOutputStream = new FileOutputStream(filePath);

            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

I'm simply trying to automate a fixed phone to take pictures. (I don't want a preview in UI, just need the image saved to SD card). I get a "Failed to connect to camera service" at the line where I call camera.open(). Most examples implement a callback with surfaces, but that is unneccessary. What am I doing wrong? I have the correct permission in my manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>

This is the class I'm using, but something is clearly wrong!

public class SimpleCameraActivity extends Activity {

    Context mContext = this;

    Button takePicture;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.setContentView(R.layout.main);

        takePicture = (Button) findViewById(R.id.takePicture);

        takePicture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                takePicture();
            }
        });

    }

    public void takePicture() {
        Camera camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        parameters.set("camera-id", 2);
        camera.setParameters(parameters);
        camera.takePicture(null, rawCallback, null);
        camera.release();
    }

    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera camera) {
            if (imageData != null) {
                saveImage(mContext, imageData, 100);
            }
        }
    };

    public void saveImage(Context mContext, byte[] imageData, int quality) {
        FileOutputStream fileOutputStream = null;

        String filePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        filePath += "/image.jpg";

        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 5;

            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length, options);

            fileOutputStream = new FileOutputStream(filePath);

            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

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

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

发布评论

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

评论(1

高跟鞋的旋律 2024-12-05 22:49:59

我不想在用户界面中预览

不支持的 UI 中进行预览。您必须具有“UI 预览”。

大多数示例都使用表面实现回调,但这是不必要的。

不,这是非常有必要的。

I don't want a preview in UI

That is not supported. You must have "a preview in UI".

Most examples implement a callback with surfaces, but that is unneccessary.

No, it is very necessary.

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