【问题标题】:Image Quality problems using Canvas and canvas.scale(Scale, Scale);使用 Canvas 和 canvas.scale(Scale, Scale) 的图像质量问题;
【发布时间】:2012-02-16 00:03:46
【问题描述】:

我在使用 Canvas 和 canvas.scale(Scale, Scale); 时遇到图像质量问题它们看起来完全像下面这样:

android:运行时调整大小的图像质量

我相信我已经阅读了所有关于重新调整位图大小时图像质量问题的帖子,但在使用 Canvas 比例(浮动比例)进行缩放时似乎没有帮助。

按照图片质量帖子的建议,我尝试了许多不同的选项。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;//I thought this would do it
CurrentPicture = BitmapFactory.decodeFile(path, options);//Also tried decodeStream()


PicturePaint = new Paint();
//PicturePaint = new Paint(Paint.FILTER_BITMAP_FLAG); //I also tried this
//PicturePaint = new Paint(Paint.ANTI_ALIAS_FLAG);  //I also tried this
canvas.scale(Scale, Scale);
canvas.drawBitmap(CurrentPicture, 0, 0, PicturePaint);

我相信这是实现我的目标的最后一个障碍。我很担心,因为如果我不能解决图像质量问题,我就有麻烦了。任何帮助表示赞赏。

谢谢!

系统不让我发图片,所以下面是一个链接。

PictureSample

【问题讨论】:

    标签: android image bitmap resize


    【解决方案1】:

    我尝试了很多很多代码。 我只是在我的清单中添加了这个,我得到了质量最好的图像。

    <uses-sdk android:minSdkVersion="9" />
    

    我认为问题在于画布是以低分辨率创建的。如果您尝试在屏幕上打印设备的分辨率,您会发现它是错误的。 随着清单中的添加,分辨率会发生变化。

    【讨论】:

      【解决方案2】:

      我不知道我的答案是否正确,我有画布代码。我希望这对你有帮助。

      public class FotosurpriseActivity extends Activity {
          /** Called when the activity is first created. */
          Bitmap overlay;
          Paint pTouch;
          int X = -100;
          int Y = -100;
          Canvas c2;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
              Bitmap mBitmapover = BitmapFactory.decodeResource(getResources(), R.drawable.ss);
              overlay = BitmapFactory.decodeResource(getResources(), R.drawable.ss).copy(Config.ARGB_8888, true);
              c2 = new Canvas(overlay);
      
              pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
              // pTouch.setXfermode(new PorterDuffXfermode(Mode.TARGET);
              pTouch.setColor(Color.TRANSPARENT);
              pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
              setContentView(new BitMapView(this, mBitmap, mBitmapover));
          }
      
          class BitMapView extends View {
              Bitmap mBitmap = null;
              Bitmap mBitmapover = null;
      
              public BitMapView(Context context, Bitmap bm, Bitmap bmover) {
                  super(context);
                  mBitmap = bm;
                  mBitmapover = bmover;
              }
      
              @Override
              public boolean onTouchEvent(MotionEvent ev) {
      
                  switch (ev.getAction()) {
      
                  case MotionEvent.ACTION_DOWN: {
      
                      X = (int) ev.getX();
                      Y = (int) ev.getY();
                      invalidate();
      
                      break;
                  }
      
                  case MotionEvent.ACTION_MOVE: {
      
                      X = (int) ev.getX();
                      Y = (int) ev.getY();
                      invalidate();
                      break;
      
                  }
      
                  case MotionEvent.ACTION_UP:
      
                      break;
      
                  }
                  return true;
              }
      
              @Override
              protected void onDraw(Canvas canvas) {
                  // called when view is drawn
                  Paint paint = new Paint();
                  paint.setFilterBitmap(true);
                  // The image will be scaled so it will fill the width, and the
                  // height will preserve the image’s aspect ration
                  /*
                   * double aspectRatio = ((double) mBitmap.getWidth()) /
                   * mBitmap.getHeight(); Rect dest = new Rect(0, 0,
                   * this.getWidth(),(int) (this.getHeight() / aspectRatio)); double
                   * aspectRatio2 = ((double) mBitmapover.getWidth()) /
                   * mBitmapover.getHeight(); Rect dest2 = new Rect(0, 0,
                   * this.getWidth(),(int) (this.getHeight() / aspectRatio2));
                   * canvas.drawBitmap(mBitmap, null, dest, paint);
                   * canvas.drawBitmap(mBitmapover, null, dest2, paint);
                   */
      
                  // draw background
                  canvas.drawBitmap(mBitmap, 0, 0, null);
                  // copy the default overlay into temporary overlay and punch a hole
                  // in it
                  c2.drawBitmap(mBitmapover, 0, 0, null); // exclude this line to show
                                                          // all as you draw
                  c2.drawCircle(X, Y, 80, pTouch);
                  // draw the overlay over the background
                  canvas.drawBitmap(overlay, 0, 0, null);
              }
          }
      
      }
      

      【讨论】:

      • 感谢您的代码。对此,我真的非常感激。我相信我有它的工作。我怀疑当我尝试 PicturePaint = new Paint(Paint.FILTER_BITMAP_FLAG); PicturePaint = 新 PaintPaint.ANTI_ALIAS_FLAG);我没有将位图设置为 Bitmap.Config.ARGB_8888
      【解决方案3】:

      您是否有可以显示您所谈论的质量问题的屏幕截图?如果在 Paint 上启用了过滤应该没问题。

      【讨论】:

      • 感谢您的回复!对于我的回复延迟,我深表歉意。我不得不休息一下,让大脑肿胀消退。系统不让我发图片,所以我添加了图片示例的链接。我将查看收到的其他回复,看看他们的 cmets 是否有帮助。
      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2020-01-13
      • 2021-11-26
      • 2016-05-10
      • 2022-01-21
      • 2013-05-19
      • 2022-08-07
      • 1970-01-01
      相关资源
      最近更新 更多