Android:以纵向模式拍照并旋转它

时间:2013-08-27 09:30:14

标签: android bitmap camera rotation fileoutputstream

我一直在与这个问题作斗争。我的相机活动被锁定为纵向模式,并且由于其他功能 - 它必须是纵向模式。用相机拍照并显示后,它会显示为旋转状态。我找到了处理这个问题的解决方案,但是对于较弱的设备需要太多的记忆,因此在点击“拍照”按钮后我必须等待大约8秒,直到它进入下一个活动(预览活动),而在我自己的Nexus 4上它无需工作立即滞后并开始。我目前的代码如下:

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile();

        if (pictureFile == null){
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d("ABC", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("ABC", "Error accessing file: " + e.getMessage());
        }

        Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
        Matrix matrix = new Matrix();
        if (currentCamera == BACK_CAMERA) {
            matrix.postRotate(90);
        } else {
            matrix.postRotate(270);
            matrix.preScale(1.0f, -1.0f);
        }
        Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        try {
            FileOutputStream outStream = new FileOutputStream(pictureFile);
            result.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (FileNotFoundException e) {
            Log.d("ABC", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("ABC", "Error accessing file: " + e.getMessage());
        }

        takenPhotoURL= pictureFile.getAbsolutePath();
        Intent intent = new Intent(CameraActivity.this, PreviewActivity.class);
        intent.putExtra("PhotoURI",takenPhotoURL);
        intent.putExtra("voteID",voteId);
        CameraActivity.this.startActivity(intent);
    }

getOutputMediaFile是一种在我的设备中生成目录的方法,其中应保存照片。 (我还没有将照片添加到画廊)。当你分析我上面发布的代码时,你可以看到我使用FileOutputStream两次。我第一次将照片保存在手机内存中,第二次再打开它,将它写在旋转的矩阵上并冲洗它。我认为使用FileOutputStream,保存 - >开场 - >保存 - >关闭 - >开场 - >保存 - 关闭正在减缓设备的疲软。我想以某种方式修改这个metod以使其更有效。请帮帮我。

我到目前为止试图删除第一个FileOutputStream并立即制作所有内容:

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile();

        if (pictureFile == null){
            return;         
        }
        try {
            FileOutputStream outStream = new FileOutputStream(pictureFile);
            outStream.write(data);

            Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());

            Matrix matrix = new Matrix();
            if (currentCamera == BACK_CAMERA) {
                matrix.postRotate(90);
            } else {
                matrix.postRotate(270);
                matrix.preScale(1.0f, -1.0f);
            }
            Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

            result.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();
        } catch (FileNotFoundException e) {
            Log.d("ABC", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("ABC", "Error accessing file: " + e.getMessage());
        }

        takenPhotoURL= pictureFile.getAbsolutePath();
        Intent intent = new Intent(CameraActivity.this, PreviewActivity.class);
        intent.putExtra("PhotoURI",takenPhotoURL);
        intent.putExtra("voteID",voteId);
        CameraActivity.this.startActivity(intent);
    }

但它不起作用。照片显示没有任何变化(没有任何反应)。而且我也尝试过Exif,但它没有效果。请帮助我提高效率和速度。

0 个答案:

没有答案