开发者

Android Simple Camera Activity

开发者 https://www.devze.com 2023-03-27 13:57 出处:网络
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

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();
        }
    }

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号