【问题标题】:Call Bitmap inside onPictureTaken在onPictureTaken里面调用Bitmap
【发布时间】:2014-11-27 07:31:04
【问题描述】:

如何在 onPictureTaken 方法中调用位图?除非我重新启动手机,否则图像不会存储在图库中

请帮忙重新安排代码。

public static int getOrientation(Context context,Uri photoUri){
    Cursor cursor = context.getContentResolver().query(photoUri,new String[] {MediaStore.Images.ImageColumns.ORIENTATION},null,null,null);
    if(cursor.getCount()!=1){
        return -1;
    }
    cursor.moveToFirst();
    return cursor.getInt(0);

}

getCorrectlyOrientatedImage() 从未使用过。请帮助我在哪里可以调用此函数。

public static Bitmap getCorrectlyOrientatedImage(Context context, Uri photoUri)throws IOException{
    InputStream is=context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is,null,dbo);
    is.close();

    int rotatedWidth,rotatedHeight;
    int orientation = getOrientation(context,photoUri);

    if(orientation == 90 || orientation ==270){
        rotatedWidth=dbo.outHeight;
        rotatedHeight=dbo.outWidth;
    }else{
        rotatedWidth=dbo.outWidth;
        rotatedHeight=dbo.outHeight;
    }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if(rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION ){
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio,heightRatio);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
    }else{
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    if(orientation > 0){
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap,0,0,srcBitmap.getWidth(),srcBitmap.getHeight(),matrix,true);
    }
    return srcBitmap;
}


private PictureCallback mPicture = new PictureCallback() {


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

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        Log.d("onPictureTaken", "File is assigned to pictureFile");
        if (pictureFile == null) {
            Log.d(DEBUG_TAG, " Error creating a media file, check storage permissions:");
            return;
        }
       try {
            Bitmap bitmap=getCorrectlyOrientatedImage()
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SELFie");
            String path = mediaStorageDir.getPath() + File.separator + "IMG_Some_name.jpg";
            SCamera.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
        } catch (FileNotFoundException e) {
            Log.d("DG_DEBUG", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
        }

    }

};

图像未存储在 SD 卡中,直到我重新启动手机并且存储的图像存储在错误的方向。如果照片是纵向拍摄的,图像会以横向存储。如何以正确的方向存储图像。

private static File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"SELFie");
    if(!mediaStorageDir.exists()){
        if(!mediaStorageDir.mkdir()){
            Log.d(DEBUG_TAG,"Filed to create directory");
            return null;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

【问题讨论】:

    标签: android android-layout android-camera


    【解决方案1】:

    我知道这个问题有点老了,但如果有人遇到同样的问题,解决方案是将图像保存在后台线程中,如this github link所示。

    private PictureCallback mPicture = new PictureCallback() {
    
        public void onPictureTaken(byte[] data, Camera camera) {
            new SaveImageTask().execute(data);
        }
    
    };
    
    
    private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
    
        @Override
        protected Void doInBackground(byte[]... data) {
    
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            Log.d("onPictureTaken", "File is assigned to pictureFile");
            if (pictureFile == null) {
                Log.d(DEBUG_TAG, " Error creating a media file, check storage permissions:");
                return null;
            }
    
           try {
                Bitmap bitmap=getCorrectlyOrientatedImage()
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SELFie");
                String path = mediaStorageDir.getPath() + File.separator + "IMG_Some_name.jpg";
                SCamera.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
            } catch (FileNotFoundException e) {
                Log.d("DG_DEBUG", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多