首页 > 代码库 > 从原始位图剪切图像
从原始位图剪切图像
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图
参数说明:
Bitmap source:要从中截图的原始位图
int x:起始x坐标
int y:起始y坐标
int width:要截的图的宽度
int height:要截的图的宽度
Bitmap.Config config:一个枚举类型的配置,可以定义截到的新位图的质量
返回值:返回一个剪切好的Bitmap
三、Bitmap剪切的封装
实际使用中,因为项目需要时常需要对基本功能进行封装,下面是一段封装的代码,仅供参考。
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; public class BitmapCut { /** * 通过资源id转化成Bitmap * * @param context * @param resId * @return */ public static Bitmap ReadBitmapById(Context context, int resId) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt.inInputShareable = true; InputStream is = context.getResources().openRawResource(resId); return BitmapFactory.decodeStream(is, null, opt); } /** * 设置背景为圆角 * * @param bitmap * @param pixels * @return */ public static Bitmap removeYuanjiao(Bitmap bitmap, int pixels) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap creBitmap = Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(creBitmap); Paint paint = new Paint(); float roundPx = pixels; RectF rectF = new RectF(0, 0, bitmap.getWidth() - pixels, bitmap.getHeight() - pixels); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, 0, 0, paint); if (!bitmap.isRecycled()) bitmap.recycle(); return creBitmap; } /** * 按正方形裁切图片 */ public static Bitmap ImageCrop(Bitmap bitmap, boolean isRecycled) { if (bitmap == null) { return null; } int w = bitmap.getWidth(); // 得到图片的宽,高 int h = bitmap.getHeight(); int wh = w > h ? h : w;// 裁切后所取的正方形区域边长 int retX = w > h ? (w - h) / 2 : 0;// 基于原图,取正方形左上角x坐标 int retY = w > h ? 0 : (h - w) / 2; Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, false); if (isRecycled && bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; } // 下面这句是关键 return bmp;// Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, // false); } /** * 按长方形裁切图片 * * @param bitmap * @return */ public static Bitmap ImageCropWithRect(Bitmap bitmap) { if (bitmap == null) { return null; } int w = bitmap.getWidth(); // 得到图片的宽,高 int h = bitmap.getHeight(); int nw, nh, retX, retY; if (w > h) { nw = h / 2; nh = h; retX = (w - nw) / 2; retY = 0; } else { nw = w / 2; nh = w; retX = w / 4; retY = (h - w) / 2; } // 下面这句是关键 Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null, false); if (bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; } return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null, // false); } /** * Bitmap --> byte[] * * @param bmp * @return */ public static byte[] readBitmap(Bitmap bmp) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos); try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return baos.toByteArray(); } /** * 将图像裁剪成圆形 * * @param bitmap * @return */ public static Bitmap toRoundBitmap(Bitmap bitmap) { if (bitmap == null) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = http://www.mamicode.com/new Rect((int) left, (int) top, (int) right, >
从原始位图剪切图像
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。