将任意图片转换成指定大小的图片

public class ImageConvertShowActivity extends Activity {
	private ImageView iv_examples, iv_method01, iv_method02;
	private static final int WIDTH = 200;
	private static final int HEIGHT = 150;
	private static final float SHADOW_RADIUS = 12.0f;
	private static final Paint SHADOW_PAINT = new Paint();
	private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_size_show);

		iv_examples = (ImageView) findViewById(R.id.iv_examples);
		iv_method01 = (ImageView) findViewById(R.id.iv_method01);
		iv_method02 = (ImageView) findViewById(R.id.iv_method02);
		iv_examples.setImageResource(R.drawable.img_01);
		Resources resources = getResources();
		iv_method01.setImageBitmap(getBitmap1(BitmapFactory.decodeResource(resources, R.drawable.img_01), WIDTH, HEIGHT));
	}

	public Bitmap getBitmap1(Bitmap bitmap, int width, int height) {
		final int bitmapWidth = bitmap.getWidth();
		final int bitmapHeight = bitmap.getHeight();

		final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

		final int scaledWidth = (int) (bitmapWidth * scale);
		final int scaledHeight = (int) (bitmapHeight * scale);

		return createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, false, SHADOW_PAINT);

	}

	private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset, boolean clipShadow, Paint paint) {
		Matrix m= new Matrix();
		final int width = src.getWidth();
		final int height = src.getHeight();
		final float sx = dstWidth / (float) width;
		final float sy = dstHeight / (float) height;
		m.setScale(sx, sy);
		Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);
		return b;
	}
	
	private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset, boolean clipShadow, Paint paint) {

		int scaledWidth = width;
		int scaledHeight = height;

		final Canvas canvas = new Canvas();
		canvas.translate(offset / 2.0f, offset / 2.0f);

		Bitmap bitmap;

		final Rect from = new Rect(x, y, x + width, y + height);
		final RectF to = new RectF(0, 0, width, height);

		if (m == null || m.isIdentity()) {
			bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
			paint = null;
		} else {
			RectF mapped = new RectF();
			m.mapRect(mapped, to);

			scaledWidth = Math.round(mapped.width());
			scaledHeight = Math.round(mapped.height());

			bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
			canvas.translate(-mapped.left, -mapped.top);
			canvas.concat(m);
		}

		canvas.setBitmap(bitmap);
		canvas.drawRect(0.0f, 0.0f, width, height, paint);
		canvas.drawBitmap(source, from, to, SCALE_PAINT);

		return bitmap;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
调用自定义函数解决题目: 1.读取自然图像、生成合成图像、高相关图像、高去相关图像,调用yasuo.m函数,完成压缩,并显示不同像素值的图像 2.读取自然图像、生成合成图像、高相关图像、高去相关图像,调用yasuo.m函数,完成压缩,并显示不同像素值的图像;调用dfff.m,得到傅里叶变换后的图像 3.读取自然图像、生成合成图像、高相关图像、高去相关图像,fft中心变换,得到傅里叶变换后的图像,调用fwhtdya2d.m,得到walsh变换后的图像,进行两种变换的对比 4.读取自然图像、生成合成图像、高相关图像、高去相关图像,调用yasuo.m函数,完成压缩,并显示不同像素值的图像;调用dfff.m,得到傅里叶变换后的图像和傅里叶变换需要的时间,显示变换后的图像,显示傅里叶变换时间随图像像素的变化 5.调用butterworthw.m,使用不同的阶数:2,5,10,显示不同阶数butterworth高通滤波和低通滤波滤波器的传递函数和处理后的图像,用于对比阶数对图像处理的影响 6.调用butterworthw.m,显示原图像、原图像频谱、低通滤波、高通滤波的传递函数,以及滤波器处理后的图像 7.读取自然图像、生成合成图像、高相关图像、高去相关图像,使用标准差为0.1、0.5、0.9的拉普拉斯高斯滤波器进行滤波处理4种图像,同时显示原图像和滤波处理后图像 8.读取自然图像,转换成灰度图片,计算其直方图,并两次增强,储存两次增强的图像,同时显示原图像、增强图像、再次增强图像和它们的直方图 9.读出自然图象,转换成灰度图片,生成合成图片,调用kirsch.m,提取图像边缘并显示,调用sobele.m,提取图像边缘并显示,同时显示自然图像、合成图像、它们kirsch提取边缘处理的图像、它们sobel提取边缘处理的图像,便于对比 10.读取自然图像、生成合成图像、高相关图像、高去相关图像,调用yasuo.m函数,完成压缩,并显示不同像素值的图像;调用dfff.m,得到傅里叶变换后的图像、傅里叶变换、傅里叶中心变换需要的时间,显示不同种类图像变换后的图像,显示傅里叶变换、傅里叶中心变换所需时间时间随图像像素的变化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值