Bitmap的getpixel(x,y)和保存到系统图库的方法

来源:互联网 发布:学外语的软件 编辑:程序博客网 时间:2024/06/11 13:02


Bitmap.getpixel(x,y);
获得这个点坐标的int 值得color


public void setPixel (int x, int y, int color)


把指定的颜色写入到位图中x,y的坐标值的位置(假设该位图是可变的)。


参数


X               待替换像素的x坐标(0到width-1)。


Y               待替换像素的y坐标(0到height-1)


color         写入到位图的颜色值


抛出


         IilegalStateException                      如果这个位图不可改变


IIlegalArgumentException   如果x,y的值超出位图的边界


 


public void setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)


用数组中的颜色值替换位图的像素值。数组中的每个元素是包装的整型,代表了颜色值。


参数


      pixels        写到位图中的颜色值


offset        从pixels[]中读取的第一个颜色值的索引


stride        位图行之间跳过的颜色个数。通常这个值等于位图宽度,但它可以更更大(或负数)


X               被写入位图中第一个像素的x坐标。


Y               被写入位图中第一个像素的y坐标


width        从pixels[]中拷贝的每行的颜色个数


height       写入到位图中的行数


异常


      IilegalStateException                      如果这个位图不可改变


IIlegalArgumentException   如果x,y,width,height的值超出位图的边界


ArrayIndexOutOfBoundsException 如果像素队


/** * bitmap中的某种颜色值替换成新的颜色 * @param oldBitmap    图片 * @param oldColor 要修改的颜色 * @param newColor 新的颜色 * @return */public static Bitmap replaceBitmapColor(Bitmap oldBitmap,int oldColor,int newColor){    //相关说明可参考 http://xys289187120.blog.51cto.com/3361352/657590/    Bitmap mBitmap = oldBitmap.copy(Bitmap.Config.ARGB_8888, true);    //循环获得bitmap所有像素点    int mBitmapWidth = mBitmap.getWidth();    int mBitmapHeight = mBitmap.getHeight();    int mArrayColorLengh = mBitmapWidth * mBitmapHeight;    int[] mArrayColor = new int[mArrayColorLengh];    int count = 0;    for (int i = 0; i < mBitmapHeight; i++) {        for (int j = 0; j < mBitmapWidth; j++) {            //获得Bitmap 图片中每一个点的color颜色值            //将需要填充的颜色值如果不是            //在这说明一下 如果color 是全透明 或者全黑 返回值为 0            //getPixel()不带透明通道 getPixel32()才带透明部分 所以全透明是0x00000000            //而不透明黑色是0xFF000000 如果不计算透明部分就都是0            int color = mBitmap.getPixel(j, i);            //将颜色值存在一个数组中 方便后面修改            if (color == oldColor) {                mBitmap.setPixel(j, i, newColor);  //将白色替换成透明色            }        }    }    return mBitmap;}
/** * 从资源文件中获取图片 * * @param context * 上下文 * @param drawableId * 资源文件id * @return */public static Bitmap gainBitmap(Context context, int drawableId) {    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),            drawableId);    return bmp;}//5.Android保存图片到系统图库public static void saveImageToGallery(Context context, Bitmap bmp) {    // 首先保存图片    File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");    if (!appDir.exists()) {        appDir.mkdir();    }    String fileName = System.currentTimeMillis() + ".jpg";    File file = new File(appDir, fileName);    try {        FileOutputStream fos = new FileOutputStream(file);        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);        fos.flush();        fos.close();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }    // 其次把文件插入到系统图库    try {        MediaStore.Images.Media.insertImage(context.getContentResolver(),                file.getAbsolutePath(), fileName, null);    } catch (FileNotFoundException e) {        e.printStackTrace();    }    // 最后通知图库更新    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" +Environment.getExternalStorageDirectory())));}


0 0
原创粉丝点击