在android中使用自定义照相机拍照

在android中使用自定义照相机拍照,android,android-camera,Android,Android Camera,我不想配置定制相机,我只需要用它拍照。 我的xml中有一个SurfaceView和一个拍照按钮。我发现这种拍照方法: mCamera.takePicture(null,null,mPicture) MPI结构定义如下: Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) {

我不想配置定制相机,我只需要用它拍照。 我的xml中有一个SurfaceView和一个拍照按钮。我发现这种拍照方法:
mCamera.takePicture(null,null,mPicture)

MPI结构定义如下:

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) {
        }
    }
};
我需要的是拍摄图像的位图,以便在其他活动中使用它。 提前谢谢

> 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;
   }
我需要的是拍摄图像的位图,以便在其他活动中使用它。提前谢谢

> 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;
   }
你们所需要的只是解码从摄像机接收到的字节数组,然后旋转它以获得正确的方向

获取相机显示方向:

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;
    }
}
我需要的是拍摄图像的位图,以便在其他活动中使用它。提前谢谢

> 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;
   }
你们所需要的只是解码从摄像机接收到的字节数组,然后旋转它以获得正确的方向

获取相机显示方向:

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

为什么要显示所有的代码!无关的您的设备中有一个jpg文件,您需要它的位图。就这些。到底是什么问题?告诉另一个avtivity文件的路径。为什么要显示所有的代码!无关的您的设备中有一个jpg文件,您需要它的位图。就这些。到底是什么问题?告诉另一位Avivity文件的路径。另一个人在这里问了这个问题:另一个人在这里问了这个问题: