0
public class camera extends Activity {
    private static final String LOG_TAG = "photo";
    private int cameraId = 1;
    private Camera camera = null;
    Button b1, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_photo);
    }

    public void takephoto() {
        camera.takePicture(null, null, pictureCallback);
    }

    @Override
    public void onResume() {
        Log.d(LOG_TAG, "onResume");
        super.onResume();
        setSurface();
    }

    private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            try {
                FileOutputStream jpg = new FileOutputStream(String.format(
                        "/sdcard/%d.jpg", System.currentTimeMillis()));
                jpg.write(data);
                jpg.close();

                Log.i(LOG_TAG, "written " + data.length + " bytes to /sdcard/JBCameraCapture.jpg");
            } catch (Exception e) {
                e.printStackTrace();
            }
            finish();
        }
    };

    private SurfaceHolder.Callback shCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.i(LOG_TAG, "surfaceDestroyed callback");
            if (camera != null) {
                camera.stopPreview();
                camera.release();
            }
            camera = null;
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.i(LOG_TAG, "surfaceCreated callback");
            startCamera(cameraId);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.i(LOG_TAG, "surfaceChanged callback ");
        }
    };

    private void setSurface() {
        SurfaceView previewSurfaceView = (SurfaceView) findViewById(R.id.preview_surface);
        previewSurfaceView.getHolder().addCallback(shCallback);
    }

    protected void startCamera(final int id) {
        new AsyncTask<Integer, Void, Camera>() {
            @Override
            protected Camera doInBackground(Integer... ids) {
                return openCamera(ids[0]);
            }

            @Override
            protected void onPostExecute(Camera c) {
                startPreview(id, c);
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        takephoto();
                    }
                }, 1000);
            }
        }.execute(id);
    }

    private static Camera openCamera(int id) {
        Log.d(LOG_TAG, "opening camera " + id);
        Camera camera = null;
        try {
            camera = Camera.open(id);
            Log.d(LOG_TAG, "opened camera " + id);
        } catch (Exception e) {
            e.printStackTrace();
            camera.release();
            camera = null;
        }
        return camera;
    }

    private void startPreview(int id, Camera c) {
        if (c != null) {
            try {
                SurfaceView previewSurfaceView = (SurfaceView) findViewById(R.id.preview_surface);
                SurfaceHolder holder = previewSurfaceView.getHolder();
                c.setPreviewDisplay(holder);
                camera = c;
                cameraId = id;
            } catch (IOException e) {
                e.printStackTrace();
                c.release();
            }
        }
    }
}

我已经为前置摄像头编写了这段代码,但它不适用于所有 Android 设备,在某些设备中它会重新启动手机,在某些设备中它显示一个不幸的错误,在某些设备中它可以正常工作。

4

0 回答 0