【问题标题】:bad quality after rescaling/resizing bitmap重新缩放/调整位图大小后质量不佳
【发布时间】:2013-11-15 05:18:30
【问题描述】:

我在重新缩放位图后遇到质量问题 我从资产中加载图像,因此解码它们必须由 Stream 完成

这是我的方法

  InputStream is = getAssets().open(lol.get(zpositions));   
Bitmap bm = BitmapFactory.decodeStream(is);
  Bitmap mBitmap    = Bitmap.createScaledBitmap(bm,width,hight, false);

我试过了

Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Config.ARGB_8888;

Bitmap bitmapsrc = BitmapFactory.decodeStream(is, null, options);
Bitmap mBitmap  = Bitmap.createScaledBitmap(bitmapsrc,width,hight, false);

但还是同样的问题

知道如何绕过这个问题吗?谢谢

这里是代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip"
android:background="@android:color/black" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
   />

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullimage);

    Display display = getWindowManager().getDefaultDisplay(); 
       width = display.getWidth();  
       height = display.getHeight(); 

       AdView adView = (AdView)findViewById(R.id.ad);
        adView.loadAd(new AdRequest());

     Intent i = getIntent();
    List<String> lol = i.getStringArrayListExtra("lol");
    this.lol = lol;
     pagerPosition = i.getExtras().getInt("id");

 {

 public Bitmap getbitBitmap() throws IOException{
    positions = MyPageChangeListener.getCurrentPage();


    if (positions == 0) {
         zpositions = pagerPosition;

    }
    InputStream is = getAssets().open(lol.get(zpositions)); 
            Options options = new BitmapFactory.Options();
            options.inScaled = false;

    Bitmap bitmapsrc = BitmapFactory.decodeStream(is, null, options);


    return bitmapsrc;

}

          private void CropWallpaper() throws IOException {
      Bitmap mBitmap    = Bitmap.createScaledBitmap(bitmapsrc,width, height, false);    
        try {
            setWallpaper(mBitmap);
            Toast.makeText(this,"Cropped successfully" ,      Toast.LENGTH_SHORT).show();   
        } catch (Exception e) {
            Toast.makeText(this,"Failed to Crop" , Toast.LENGTH_SHORT).show();
            // TODO: handle exception
        }
}

【问题讨论】:

  • 你是想增加还是减少?增加必然会对质量产生负面影响。在某种程度上,减少也会,但这些通常可以通过使用混叠来解决。
  • 我们需要知道您“assets”文件夹中资源图片的宽度和高度,以及您发送给Bitmap.createScaledBitmap()的宽度和高度。
  • 我正在尝试调整图像大小以适应屏幕大小我知道位图宽度/高度也屏幕宽度/大小,但使用 Bitmap.createScaledBitmap() 在调整大小后降低质量是我的问题跨度>

标签: android bitmap resize


【解决方案1】:

当增加图像尺寸时,图像质量总是明显下降到一定程度。因此,当您缩放图像以“适合屏幕大小”时,“assets”文件夹中资源图像的分辨率小于显示它的屏幕。

解决方案是使用至少与您计划在其上显示的最大屏幕大小相同的图像。您可以通过两种方式做到这一点:

  1. 找到现在在您的“资产”中的图像的原始来源 文件夹并将其调整为相对较大的android的大小 设备屏幕,例如 1280x800。不要变得更大,因为你 当您尝试打开图片时会收到一个OutOfMemoryError
  2. 如果您的图片已经很小并且您无法访问更大的图片 原始副本,然后我建议在 Photoshop 或使用 放入您的“资产”之前的另一个桌面成像产品 文件夹。这些影像产品将更好地提升您的形象 远超 Android 操作系统的实时能力。

最后,如果您遇到OutOfMemoryError,您可以选择:

  1. 减小图像大小,直到找到可接受的平衡 在尺寸和显示质量之间,或
  2. 将您的图片从“assets”文件夹中移动到多个不同大小的图片中 标准 “drawable-hdpi”、“drawable-mdpi”等目录结构。 操作系统将在这些目录之一中打开一个图像,该目录位于 根据屏幕密度, 更可能与堆内存容量成正比 硬件。

当然,您也可以在放大时使用BitmapFactory.Options,例如 32 位 (ARGB444) 和抖动,但这只会在增加显示图像的大小时帮助很大。

【讨论】:

  • 好吧,我使用的是高清画质,但质量仍然很差,而且我正在使用内存/磁盘缓存,所以内存不足错误很少见
  • 但是如何使用 bitmapfactory 选项进行流解码然后重新缩放?
  • 暂时偏离轨道,但这可能比BitmapFactory.Options 更重要。我认为您根本不应该在您的应用程序中扩展您的Bitmap。打开任何Bitmap 后,使用您的布局来控制显示缩放(即XML android:layout_width="match_parent")。您可能还必须使用android:scaleType="fitCenter" 或类似名称。所以,回到开头,从InputStream 打开您的图像并按照此评论显示它。您可能还想使用Bitmap.Config.ARGB_8888
  • 我想缩放图像,以便将其设置为墙纸,我认为这不适用于设置墙纸
  • 不客气。您可以在此之前的选项中设置RGB_8888Bitmap bitmapsrc = BitmapFactory.decodeStream(is, null, options);。但是,您最好的改进是按照答案中的建议将更大的图像放入您的“资产”中。
【解决方案2】:

由于您已经尝试了BitmapOptions的所有组合,请尝试在创建缩放位图时设置过滤器参数。

Bitmap mBitmap  = Bitmap.createScaledBitmap(bitmapsrc,width,hight, true);

放大时会平滑图像。

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2018-06-15
    • 2012-11-30
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多