在Android

时间:2017-03-14 18:09:52

标签: android android-camera

我不是要配置自定义相机,我只需要用它拍照。 我的xml中有一个SurfaceView,还有一个用于拍照的按钮。我发现这种方法可以拍照: mCamera.takePicture(null, null, mPicture);

mPicture定义如下:

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();


        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

我需要的是拍摄图像的位图,以便在另一个Activity中使用它。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

> I asked my instructor about this question and he told me that the
> code below should work fine for you.

public class Custom_Camera
{
private Camera a;
private ab1 ab2;

/** Called when the activity is first created. */
@Override
public void onCreate(Total State) {
    super.onCreate(Saved State);
    setContentView(R.layout.main);
    a = getCameraInstance();
    ab1 = new ab2(this, a);
    FrameLayout= (FrameLayout) findViewById(R.id.a_b2);
    preview.addView(ab2);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }
    });
}
private Camera a getCameraInstance() {
    Camera a = null;
    try {
        camera = Camera.open();
    } catch (Exception e) {
    }
    return camera a;
}

Picture = new Picture() {
    public void taken(byte[] data, Camera camera) {
        File = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
   }

答案 1 :(得分:0)

  

我需要的是拍摄图像的位图,以便在另一个Activity中使用它。提前谢谢!

您只需要解码从相机接收的字节数组,然后将其旋转为正确方向。

获取相机显示方向:

private static int getCameraDisplayOrientation(int cameraId, Activity activity) {
    int rotation = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay().getRotation();
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + rotation) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - rotation + 360) % 360;
    }
    return result;
}

将字节数组解码为位图:

public static Bitmap decodeByteArray(byte[] data, float rotationDegree) {
    try {
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
        if (rotationDegree != 0) {
            bm = createRotatedBitmap(bm, rotationDegree);
        }
        return  bm;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

旋转位图:

public static Bitmap createRotatedBitmap(Bitmap bm, float rotation) {
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation, bm.getWidth()/2, bm.getHeight()/2);
    try {
        Bitmap result =  Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return result;
    } catch (OutOfMemoryError e) {
        return null;
    }
}