takePicture()总是冻结几秒钟

时间:2012-10-23 08:56:32

标签: android android-camera

当我按下CameraActivity

上的快门按钮时,我遇到了两件奇怪的事
  1. 在设备2.3和4.0(Galaxy Ace II,Nexus One和Galaxy Nexus)上, 它将冻结约2秒钟,然后继续nextActivity

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

  3. garbled pixels

    起初我认为我的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();
            }
    
        }
    };
    

1 个答案:

答案 0 :(得分:0)

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