1

当我按下我的快门按钮时,我遇到了 2 件奇怪的事情CameraActivity

  1. 在设备 2.3 和 4.0(Galaxy Ace II、Nexus One 和 Galaxy Nexus)上,它将冻结大约 2 秒,然后继续运行nextActivity

  2. 只有 Galaxy Ace 2.3.6,它会在冻结期间给我一个像下图这样的乱码,但它可以在稍后给我正确的图像nextActivity

乱码像素

起初我认为我的方法可能有问题或耗时PicutureCallback,但看起来没问题?而且我不明白为什么这个乱码只会出现在 Galaxy Ace 上。

那么会有更好的方法来处理它PictureCallback以使其更快吗?

没有错误日志,所以我只是把我的相关代码:

// I call the takePicture in my button listener.
camera.takePicture(null, null, mPicture);

// here's the picture callback
private PictureCallback mPicture = new PictureCallback() {

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


        new ShutterHandler(data).execute();
    }

    class ShutterHandler extends AsyncTask<Void, Void, Void> {

        Bitmap bm;
        String timeStamp;
        String filename;
        Intent nextIntent = new Intent(CameraActivity.this, nextActivity.class);

        // a tricky way to pass data into the AsyncTask
        public ShutterHandler(byte[] data) {
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);

        }


        @Override
        protected Void doInBackground(Void... params) {
            // simply generate a time stamp as filename
            timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            filename = timeStamp + ".jpg";
            Uri path;
            try {

                // save file to app's private folder
                FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);

                bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);              
                // retrieve Uri from the file
                path = Uri.fromFile(getFileStreamPath(filename));

                Log.d("CameraActivity", "file saved to: " + path);
                fos.flush();
                fos.close();

                // launch ShowMarkersActivity
                String pathString = path.getPath();
                nextIntent.putExtra("IMG", pathString);
                nextIntent.putExtra("FileName", filename);
                nextIntent.putExtra("FLAG", true);

            } catch (FileNotFoundException e) {
                Log.d("CameraActivity", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("CameraActivity", "Error accessing file: " + e.getMessage());
            }
            return null;                        
        }
        protected void onPostExecute(Void v) {
            CameraActivity.this.startActivity(nextIntent);
            finish();
        }

    }
};
4

1 回答 1

0

如果你要求相机图片回调返回jpeg格式,它会明显更快。在大多数可用设备上,图片回调仍然很慢。

于 2012-10-27T04:30:50.083 回答