使用相机类捕获图像

时间:2015-10-09 11:29:03

标签: android camera android-camera

我想使用相机类捕获图像,然后在表面视图中显示它。最初,我遇到了相机在横向视图中操作的问题,所以我添加了一种方法来设置相机的方向。现在相机正常运行,但拍摄的图像旋转。

鉴于以下是我的代码;

public class MainActivity extends AppCompatActivity {

    private ImageView imageResult;
    private CameraPreview cameraView;
    private FrameLayout frameNew;
    private Button snapPhoto;
    boolean takePicture = false;
    private Bitmap image = null;
    private static final String TAG = "MyActivity";


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

        setupCamera();
    }

    public void setupCamera() {
        cameraView = new CameraPreview(getApplicationContext(), this);

        imageResult = new ImageView(getApplicationContext());
        imageResult.setBackgroundColor(Color.GRAY);

        frameNew = (FrameLayout) findViewById(R.id.frame);
        snapPhoto = (Button) findViewById(R.id.capture);

        frameNew.addView(imageResult);
        frameNew.addView(cameraView);
        frameNew.bringChildToFront(imageResult);
    }

    public void captureHandler(View view) {
        if (takePicture) {

            cameraView.capture(jpegHandler);

        } else {
            takePicture = true;
            frameNew.bringChildToFront(cameraView);
            imageResult.setImageBitmap(null);

            snapPhoto.setText("Capture");

        }
    }

    public Camera.PictureCallback jpegHandler = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            image = BitmapFactory.decodeByteArray(data, 0, data.length);
            imageResult.setImageBitmap(image);
            frameNew.bringChildToFront(imageResult);

            snapPhoto.setText("take a picture");
            takePicture = false;

            File pictureFile = getOuputMediaFile();

            if (pictureFile == null) {
                Log.d(TAG, "Error creating media file ");
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    public File getOuputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Divyeshs");

        if (!mediaStorageDir.exists()) {
            Log.d("Divyeshs", "failed to create directory");
            return null;
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
        return mediaFile;
    }

}

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback2 {

    private SurfaceHolder mHolder;
    public Camera camera = null;
    Activity activity;
    int camid;

    public CameraPreview(Context context, Activity activity) {
        super(context);
        this.activity = activity;
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    @Override
    public void surfaceRedrawNeeded(SurfaceHolder holder) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        int camCount;

        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        camCount = Camera.getNumberOfCameras();

        for (int i = 0; i < camCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    camera = Camera.open(i);
                    camid = i;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            camera.setPreviewDisplay(mHolder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        camera.startPreview();
        CameraPreview.setCameraDisplayOrientation(activity, camid, camera);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera = null;
    }

    public void capture(Camera.PictureCallback jpegHandler) {
        camera.takePicture(null, null, jpegHandler);
    }

    public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
        Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);
    }

}

0 个答案:

没有答案