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



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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值