从原始位图剪切图像

  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 = new Rect((int) left, (int) top, (int) right,
                    (int) bottom);
            final Rect dst = new Rect((int) dst_left, (int) dst_top,
                    (int) dst_right, (int) dst_bottom);
            final RectF rectF = new RectF(dst);  

            paint.setAntiAlias(true);  

            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  

            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, src, dst, paint);
            if (bitmap != null && !bitmap.isRecycled())
            {
                bitmap.recycle();
                bitmap = null;
            }
            return output;
        }  

        // 将图片变成带圆边的圆形图片
        public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height)
        {
            if (bitmap == null)
            {
                return null;
            }
            // 将图片变成圆角
            Bitmap roundBitmap = Bitmap.createBitmap(width, height,
                    Config.ARGB_8888);
            Canvas canvas = new Canvas(roundBitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            int len = (width > height) ? height : width;
            canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true);
            canvas.drawBitmap(scaledBitmap, 0, 0, paint);
            // 将图片加圆边
            Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            canvas = new Canvas(outBitmap);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(0xffffffff);
            canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
            canvas.drawBitmap(roundBitmap, 0, 0, paint);
            bitmap.recycle();
            bitmap = null;
            roundBitmap.recycle();
            roundBitmap = null;
            scaledBitmap.recycle();
            scaledBitmap = null;
            return outBitmap;
        }  

        // 将图片变成带圆边的圆形图片
        public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height,
                int color)
        {
            if (bitmap == null)
            {
                return null;
            }
            // 将图片变成圆角
            Bitmap roundBitmap = Bitmap.createBitmap(width, height,
                    Config.ARGB_8888);
            Canvas canvas = new Canvas(roundBitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            int len = (width > height) ? height : width;
            canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true);
            canvas.drawBitmap(scaledBitmap, 0, 0, paint);
            // 将图片加圆边
            Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            canvas = new Canvas(outBitmap);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
            canvas.drawBitmap(roundBitmap, 0, 0, paint);
            bitmap.recycle();
            bitmap = null;
            roundBitmap.recycle();
            roundBitmap = null;
            scaledBitmap.recycle();
            scaledBitmap = null;
            return outBitmap;
        }  

        /**
         * function:图片转圆角
         *
         * @param bitmap
         *            需要转的bitmap
         * @param pixels
         *            转圆角的弧度
         * @return 转圆角的bitmap
         */
        public static Bitmap toRoundCorner(Bitmap bitmap, int pixels)
        {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);
            final float roundPx = pixels;
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            if (bitmap != null && !bitmap.isRecycled())
            {
                bitmap.recycle();
            }
            return output;
        }  

        /**
         * 获取指定的圆角图片
         *
         * @param bitmap
         * @return
         */
        public static Bitmap getRadiusBitmap(Bitmap bitmap)
        {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(0xffffffff);
            Bitmap radiusBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(radiusBitmap);
            RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
            canvas.drawRoundRect(rectF, 7, 7, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, 0, 0, paint);
            if (bitmap != null && !bitmap.isRecycled())
            {
                bitmap.recycle();
            }
            return radiusBitmap;
        }  

        // 获得指定大小的圆边的bitmap数组
        public static ArrayList<Bitmap> getRadiusBitmapList(String[] pathArray,
                int size, int len, float radius, int color)
        {
            Bitmap canvasBitmap = null;
            Canvas canvas = null;
            Paint paint = null;
            RectF rectF = new RectF(0, 0, len - radius, len - radius);
            File file = null;
            FileInputStream fis = null;
            Bitmap bitmap = null;
            Bitmap scaledBitmap = null;  

            ArrayList<Bitmap> list = new ArrayList<Bitmap>();
            for (int i = 0; i < pathArray.length; i++)
            {
                file = new File(pathArray[i]);
                if (!file.exists())
                    continue;
                try
                {
                    fis = new FileInputStream(file);
                    bitmap = BitmapFactory.decodeStream(fis);
                    if (bitmap != null)
                    {
                        canvasBitmap = Bitmap.createBitmap(len, len,
                                Config.ARGB_8888);
                        canvas = new Canvas(canvasBitmap);
                        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                        paint.setColor(color);
                        canvas.drawRoundRect(rectF, radius, radius, paint);
                        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  

                        scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len,
                                true);
                        canvas.drawBitmap(scaledBitmap, 0, 0, paint);
                        list.add(canvasBitmap);
                    }
                } catch (FileNotFoundException e)
                {
                } finally
                {
                    if (fis != null)
                    {
                        try
                        {
                            fis.close();
                        } catch (IOException e1)
                        {
                        }
                    }
                }
                if (list.size() == size)
                    break;
            }
            if (scaledBitmap != null && !scaledBitmap.isRecycled())
            {
                scaledBitmap.recycle();
                scaledBitmap = null;
            }
            if (bitmap != null && !bitmap.isRecycled())
            {
                bitmap.recycle();
                bitmap = null;
            }
            return list;
        }  

        /**
         * 按照一定的宽高比例裁剪图片
         *
         * @param bitmap
         * @param num1
         *            长边的比例
         * @param num2
         *            短边的比例
         * @return
         */
        public static Bitmap ImageCrop(Bitmap bitmap, int num1, int num2,
                boolean isRecycled)
        {
            if (bitmap == null)
            {
                return null;
            }
            int w = bitmap.getWidth(); // 得到图片的宽,高
            int h = bitmap.getHeight();
            int retX, retY;
            int nw, nh;
            if (w > h)
            {
                if (h > w * num2 / num1)
                {
                    nw = w;
                    nh = w * num2 / num1;
                    retX = 0;
                    retY = (h - nh) / 2;
                } else
                {
                    nw = h * num1 / num2;
                    nh = h;
                    retX = (w - nw) / 2;
                    retY = 0;
                }
            } else
            {
                if (w > h * num2 / num1)
                {
                    nh = h;
                    nw = h * num2 / num1;
                    retY = 0;
                    retX = (w - nw) / 2;
                } else
                {
                    nh = w * num1 / num2;
                    nw = w;
                    retY = (h - nh) / 2;
                    retX = 0;
                }
            }
            Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
                    false);
            if (isRecycled && bitmap != null && !bitmap.equals(bmp)
                    && !bitmap.isRecycled())
            {
                bitmap.recycle();
                bitmap = null;
            }
            return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
                        // false);
        }
    }
时间: 2024-10-14 11:10:09

从原始位图剪切图像的相关文章

nodejs 剪切图像在上传,并保存到指定路径下(./public/img/&#39; + req.session.token + &#39;.jpg‘)

前jQuery端接收数据 function upAvatar(img){ console.log(img); // data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMD…/7vA+eq/xZAlzs/wBYlP8AtlpRRXcc9h++B/8AVyJRs30UVRkM2e9M8s+tFFSBE6UUUUGp/9k= $.ajax({ url:'/user/upload', type:'PUT',

Android--启动系统的剪切图像功能并返回结果

直接上代码: 1 //启动裁剪图片 2 private void cropPhotoUri(Uri uri){ 3 Intent intent = new Intent("com.android.camera.action.CROP"); 4 intent.setDataAndType(uri,"image/*"); //设置裁剪类型 5 //设置裁剪 6 intent.putExtra("crop","true"); 7 /

Android实现位图剪切

我们不能总是依赖于BitmapFactory 以下告诉大家怎么从Bitmaqp中截取某一部分创建新的Bitmap 系统会有一个默认png图片:icon.png 可是这个图片中最外层会有白色的 比較讨厌 如今以此为例 说说怎么截取 由于其外层为白色 显示不出来 所以我用了 *.9.png 作为其边界 创建Bitmaop 且指向icon.png <span style="font-size:12px;">Bitmap ori = BitmapFactory.decodeReso

Android_(游戏)打飞机03:控制玩家飞机

Android_(游戏)打飞机01:前言 传送门 Android_(游戏)打飞机02:游戏背景滚动 传送门 Android_(游戏)打飞机03:控制玩家飞机 传送门 控制玩家飞机效果 package com.example.administrator.myapplication; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import

canvas图像以及剪切

图像篇: 代码: 1 /** 2 * Created by Administrator on 2016/1/28. 3 */ 4 function draw (id){ 5 var canvas = document.getElementById(id); 6 var context = canvas.getContext("2d"); 7 var newImg = new Image(); 8 newImg.src = "女孩.jpg"; 9 newImg.onl

Android实现对图片的缩放、剪切、旋转、存储

Android实现对图片的缩放.剪切.旋转.存储 一.问题描述 在开发中,当我们需要的有一张大图片同时还需要一些小图片时,我们只需要通过代码对此图片进行不同比例的缩放即可,这样大大节约资源,减小了安装包的尺寸 .除缩放外,我们还经常对图片进行其他操作如裁剪.旋转.存储等. 这样我们可以编写对于图片进行处理的通用组件,方便开发.下面就分享一下对图片进行处理的组件BitmapUtil,案例界面: 二.技术点描述 1.通过BitmapFactory取得Bitmap Bitmap bm=BitmapFa

windowsphone8.1学习笔记之位图编程

说位图,先把image控件简单过下,Image的Source设置 <Image Name="img" Source="可以是网络图片的Uri.应用文件的Uri或者安装包文件的Uri" /> img.Source = new BitmapIamge(new Uri(同上)); Image的Stretch属性指定图像如何填充,枚举定义: None,图像不拉伸,一合适尺寸显示: Uniform,保留图像的纵横比,按控件的大小输出图像: UnifToFill,已

spice 图像压缩算法相关代码逻辑流程

下面是转载http://blog.csdn.net/zhoujiaxq/article/details/11201893 内容,是对图像算法的简单介绍接流程 目前的spice图像压缩主要采用了quic,glz和jpeg.quic和glz是无损压缩算法,quic主要用于照片,glz用于人工图像,jpeg也主要用于照片压缩但是是有损的.jpeg能节省50%的带宽,glz只能节省20%,但是jpeg会带来更大的开销,所以不能都使用jpeg进行压缩. spice官网对于广域网支持的介绍:http://s

VC++实现位图显示透明效果--实现原理

我们在进行程序的界面设计时,常常希望将位图的关键部分,也既是图像的前景显示在界面上,而将位图的背景隐藏起来,将位图与界面很自然的融合在一起,本文介绍了透明位图的制作知识,并将透明位图在一个对话框中显示了出来.本文所使用的原始位图及程序编译运行后的界面效果如下图所示: 图一.原始位图 图二.对话框界面上透明显示位图 一.实现方法 绘制"透明"位图是指绘制某一位图中除指定颜色外的其余部分,我们称这种颜色为"透明色".通过将位图的背景色指定为"透明色"