0

我一直在与这个问题作斗争一段时间。我的相机活动被锁定为纵向模式,并且由于它的其他功能 - 它必须是纵向模式。在我用我的相机拍照并显示它后,它看起来是旋转的。我找到了解决此问题的解决方案,但它占用了较弱设备的太多内存,因此在单击“拍照”按钮后,我必须等待大约 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,保存 -> 打开 -> 保存 -> 关闭 -> 打开 -> 保存 - 关闭正在减慢较弱的设备。我想以某种方式修改此方法以使其更有效。请帮帮我。

到目前为止,我尝试删除第一个 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,但没有效果。请帮助我提高效率和速度。

4

0 回答 0