保存图像并显示它

时间:2016-07-07 16:52:10

标签: android

我有一个名为" SaveImageTask"的内部类。它在后台运行并从摄像机保存拍摄的图像,然后我将Intent用于另一个名为" MainActivity2"的类。我想在哪里显示图像,这里是代码:

 private class SaveImageTask extends AsyncTask<byte[], Void, Void> {

    @Override
    protected Void doInBackground(byte[]... data) {
        FileOutputStream outStream = null;
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/SelfieLightCamera");
            dir.mkdirs();
            String fileName = String.format("%d.jpg", System.currentTimeMillis());
            File outFile = new File(dir, fileName);
            outStream = new FileOutputStream(outFile);
            b = flip(BitmapFactory.decodeByteArray(data[0], 0, data[0].length));
            b.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
            Log.d("pictureTaken", "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
            refreshGallery(outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        CameraPreview.safeToTakePicture = true;
        startActivity(new Intent(MainActivity.this, MainActivity2.class));
        return null;
    }

    public Bitmap flip(Bitmap bitmap) {

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
        Matrix matrixMirrorY = new Matrix();
        matrixMirrorY.setValues(mirrorY);
        matrix.postConcat(matrixMirrorY);
        matrix.postRotate(90);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    }
}

MainActivity2.class:

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ImageView img = (ImageView) findViewById(R.id.imageView);
        img.setImageBitmap(MainActivity.b);
    }
}

然而,它需要花费太多时间,我拍摄的图像然后显示延迟5秒之间,以任何方式使其更有效率?

1 个答案:

答案 0 :(得分:0)

解决方案是在保存之前显示图像(之前将Intent调用到其他类):

private class SaveImageTask extends AsyncTask<byte[], Void, Void> {

        @Override
        protected Void doInBackground(byte[]... data) {
            FileOutputStream outStream = null;
            try {
                b = flip(BitmapFactory.decodeByteArray(data[0], 0, data[0].length));
                startActivity(new Intent(MainActivity.this, MainActivity2.class));
                File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File(sdCard.getAbsolutePath() + "/SelfieLightCamera");
                dir.mkdirs();
                String fileName = "Picture_" + String.format("%d.jpg", System.currentTimeMillis());
                File outFile = new File(dir, fileName);
                outStream = new FileOutputStream(outFile);
                b.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();
                Log.d("pictureTaken", "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
                refreshGallery(outFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            CameraPreview.safeToTakePicture = true;
            return null;
        }

        public Bitmap flip(Bitmap bitmap) {

            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
            Matrix matrix = new Matrix();
            float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
            Matrix matrixMirrorY = new Matrix();
            matrixMirrorY.setValues(mirrorY);
            matrix.postConcat(matrixMirrorY);
            matrix.postRotate(90);
            return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        }
    }