使用 Canvas 和 canvas.scale(Scale, Scale); 的图像质量问题

发布于 2024-12-28 07:28:29 字数 1042 浏览 1 评论 0原文

我在使用 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

I am having Image Quality problems using Canvas and canvas.scale(Scale, Scale); they look exactly like the following:

android: quality of the images resized in runtime

I believe I have read all the posts on image quality problems when re-sizing bitmaps, but it doesn't seem to help when scaling with a Canvas scale(float scale).

I have tried many different options as suggested by the image quality posts.

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);

I believe this the last barrier to achieving my goals. I am quite concerned as I am in trouble if I can't get the image quality problem solved. Any help is appreciated.

Thanks!

The system will not let me post a picture, so following is a link.

PictureSample

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪落纷纷 2025-01-04 07:28:29

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

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);
        }
    }

}

I don't no whether my answer is correct or not i have a code for canvas. i wish this might help you.

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);
        }
    }

}
寒尘 2025-01-04 07:28:29

您有可以显示您所说的质量问题的屏幕截图吗?如果在画图上启用过滤应该没问题。

Do you have a screenshot that would show the quality issue you are talking about? If filtering is enabled on the Paint it should be fine.

何时共饮酒 2025-01-04 07:28:29

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

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

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

I tried many and many code.
I just add this in my manifest and I had the best quality image.

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

I think the problem is the canvas is created in low resolution. If u try to print on the screen the resolution of your device, u can see it is wrong.
With the add in the manifest the resolution change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文