【问题标题】:android-bitmap low quality issueandroid-bitmap 低质量问题
【发布时间】:2015-08-18 08:07:11
【问题描述】:

我刚刚创建了一些函数来从相机拍照并将结果图像放入 imageview。我发现我拍出来的照片质量太差了。

这里有一些代码:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            picture.setImageBitmap(photo);
        }
    }

View.OnClickListener camerabtnlistener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    };


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_help_3);
        btnPicture = (Button) findViewById(R.id.btnPicture);
        picture= (ImageView) findViewById(R.id.picture);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        layoutPic = (RelativeLayout) findViewById(R.id.layoutPic);
        setSupportActionBar(mToolbar);

        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        btnPicture.setOnClickListener(camerabtnlistener);

    }

和xml

<ImageView
            android:id="@+id/picture"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/test_admin_big"
            android:adjustViewBounds="true"/>

我已经在下面尝试了这些解决方案,但那不起作用

  1. Resizing ImageView to fit to aspect ratio
  2. Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?
  3. Android bitmap quality issues

请指教

谢谢

【问题讨论】:

    标签: java android xml bitmap


    【解决方案1】:

    如果你使用 Bundle extras = data.getExtras();在您的 onActivityResult 中,它将返回缩略图而不是实际图像。

    这是我用于捕获和保存相机图像然后将其显示到 imageview 的代码。

    您必须将相机图像保存到特定位置,然后从该位置获取。

    这里是打开捕获相机图像活动的方法。

    private static final int CAMERA_PHOTO = 111;
    private Uri imageToUploadUri;
    
    private void captureCameraImage() {
            Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
            chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            imageToUploadUri = Uri.fromFile(f);
            startActivityForResult(chooserIntent, CAMERA_PHOTO);
        }
    

    那么你的 onActivityResult() 方法应该是这样的。

      @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
    
                if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) {
                    if(imageToUploadUri != null){
                        Uri selectedImage = imageToUploadUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
                        if(reducedSizeBitmap != null){
                            imageview.setImageBitmap(reducedSizeBitmap);
                        }else{
                            Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                        }
                    }else{
                        Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                    }
                } 
            }
    

    这里是 onActivityResult() 中使用的 getBitmap() 方法。

    private Bitmap getBitmap(String path) {
    
            Uri uri = Uri.fromFile(new File(path));
            InputStream in = null;
            try {
                final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
                in = getContentResolver().openInputStream(uri);
    
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(in, null, o);
                in.close();
    
    
                int scale = 1;
                while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                        IMAGE_MAX_SIZE) {
                    scale++;
                }
                Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
    
                Bitmap b = null;
                in = getContentResolver().openInputStream(uri);
                if (scale > 1) {
                    scale--;
                    // scale to max possible inSampleSize that still yields an image
                    // larger than target
                    o = new BitmapFactory.Options();
                    o.inSampleSize = scale;
                    b = BitmapFactory.decodeStream(in, null, o);
    
                    // resize to desired dimensions
                    int height = b.getHeight();
                    int width = b.getWidth();
                    Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);
    
                    double y = Math.sqrt(IMAGE_MAX_SIZE
                            / (((double) width) / height));
                    double x = (y / height) * width;
    
                    Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                            (int) y, true);
                    b.recycle();
                    b = scaledBitmap;
    
                    System.gc();
                } else {
                    b = BitmapFactory.decodeStream(in);
                }
                in.close();
    
                Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
                        b.getHeight());
                return b;
            } catch (IOException e) {
                Log.e("", e.getMessage(), e);
                return null;
            }
        }
    

    希望对你有帮助!

    【讨论】:

    • 谢谢,我现在知道我使用缩略图显示在 imageview 中,所以我按照您的建议使用实际图像。谢谢
    • 有一个错误:找不到用户 0 的提供者;预计会为此权限找到有效的 ContentProvider
    【解决方案2】:

    而不是这样简单地创建您自己的文件夹并在为相机创建意图时提供其 uri,例如: Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); fileUri = GetOutpoutMediaFileUri.getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(intent, 0)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多