图象处理相关技术

一、获取屏幕的宽高:
1.通过getWidth(),getHeight()获取,不过该方法已被弃用:

WindowManager windowManager = getWindowManager();
        screenWidth = windowManager.getDefaultDisplay().getWidth();
        screenHeight = windowManager.getDefaultDisplay().getHeight();

2.通过getSize(point),该方法在api13时引入:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            windowManager.getDefaultDisplay().getSize(point);
            screenWidth  = point.x;
            screenHeight  = point.y;
            Log.e(TAG, "width : " + width + ", height : " + height);
        }

二、截取图片某一部分并按屏幕宽高比放大:

 private void displayBitmap()
    {
        try {
            InputStream inputStream = getAssets().open("chemo.jpg");
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(inputStream, null, options);

            int width = options.outWidth;
            int height = options.outHeight;

            options.inJustDecodeBounds = false;
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
            originalImgView.setImageBitmap(bitmap);  // 显示原图

            BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
            // 截取图片某部分
            bitmap = bitmapRegionDecoder.decodeRegion(new Rect(width/3 - 100, height/3 - 100, width/3 + 100, height/3 + 100), options);

            int bitmapWidth = bitmap.getWidth();
            int bitmapHeight = bitmap.getHeight();
            Log.e(TAG, "bitmapWidth : " + bitmapWidth + ", bitmapHeight : " + bitmapHeight);

            int xScale = screenWidth/bitmapWidth;
            int yScale = screenHeight/bitmapHeight;

            // 利用矩阵缩放截取出来的那部分图片
            Matrix matrix = new Matrix();
            matrix.postScale(xScale, yScale);
            Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

            showImgView.setImageBitmap(resizeBmp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

效果图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值