简体   繁体   中英

Image Quality problems using Canvas and canvas.scale(Scale, Scale);

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

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

}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM