位图的像素操作练习(2012-05-25)

很久没有较长篇的代码练习了,今天偷闲回顾练习了一下位图文件的像素操作,备忘,参考了:

水平翻转,垂直翻转,转灰度:

void BMPOpt(){
BITMAPFILEHEADER fhd;
BITMAPINFOHEADER ihd;
BYTE *pDat=NULL;
CString sfnm("e:\\t.bmp");
CFile file;
if(file.Open(sfnm, CFile::modeRead)){
try{
file.Read(&fhd, sizeof(BITMAPFILEHEADER));
file.Read(&ihd, sizeof(BITMAPINFOHEADER));
int offset = fhd.bfOffBits;
int high = ihd.biHeight,
wide = ihd.biWidth,
w4B  = ((wide*3+3)/4)*4;
int bits = ihd.biBitCount;


int len = high*w4B + w4B*2; //*wide*3;
pDat = (BYTE*)malloc(len);
if(pDat){
memset(pDat, 0, len);
}else{
throw "malloc err";
}
BYTE *pLine = pDat+high*w4B;


file.Read(pDat, len);
file.Close();
//{{// flip H::
for(int i=0; i<high/2; i++){
memcpy(pLine, pDat+i*w4B, w4B);
memcpy(pDat+i*w4B, pDat+(high-1-i)*w4B, w4B);
memcpy(pDat+(high-1-i)*w4B, pLine, w4B);
}


CFile f2;
if(f2.Open("e:\\t2.bmp", CFile::modeCreate|CFile::modeWrite)){
f2.Write(&fhd, sizeof(BITMAPFILEHEADER));
f2.Write(&ihd, sizeof(BITMAPINFOHEADER));
f2.Write(pDat, high*w4B);
f2.Close();
}
//}// flip H;;
//{ flip V::
BYTE a=0, b=0, c=0;
for(int i=0; i<high; i++){
pLine = pDat+i*w4B;

for(int j=0; j<wide/2; j++){
a=pLine[j*3];
b=pLine[j*3+1];
c=pLine[j*3+2];


int pos = (wide-1-j)*3;
pLine[j*3]   = pLine[pos];
pLine[j*3+1] = pLine[pos+1];
pLine[j*3+2] = pLine[pos+2];


pLine[pos]   = a;
pLine[pos+1] = b;
pLine[pos+2] = c;
}
}


CFile f3;
if(f3.Open("e:\\t3.bmp", CFile::modeCreate|CFile::modeWrite)){
f3.Write(&fhd, sizeof(BITMAPFILEHEADER));
f3.Write(&ihd, sizeof(BITMAPINFOHEADER));
f3.Write(pDat, high*w4B);
f3.Close();
}
//}} flip V;;
//{//make gray bmp, 8bits:
int gwide = (wide+3)/4*4;
int glen = high*gwide;
BYTE *pdat2 = (BYTE*)malloc(glen+4);
memset(pdat2, 0, glen);
BYTE *p8ln = NULL;
BYTE *p24ln = NULL;
//BYTE a=0, b=0, c=0;
for(int i=0; i<high; i++){
p8ln = pdat2 + i*gwide;
p24ln = pDat + i*w4B;

for(int j=0; j<wide; j++){
a=p24ln[j*3];
b=p24ln[j*3+1];
c=p24ln[j*3+2];


p8ln[j] = (a+b+c)/3;
}
}


RGBQUAD rgb[256] = {0};
int rgblen = sizeof(rgb);
fhd.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+gwide*high + sizeof(rgb);

ihd.biBitCount = 8;
ihd.biSize = sizeof(BITMAPINFOHEADER);
ihd.biSizeImage = 0;
ihd.biClrUsed = 256;
for(int n=0; n<0xff; n++){
rgb[n].rgbBlue = n;
rgb[n].rgbGreen = n;
rgb[n].rgbRed  = n;
}


CFile f4;
if(f4.Open("e:\\t4.bmp", CFile::modeCreate|CFile::modeWrite)){
f4.Write(&fhd, sizeof(BITMAPFILEHEADER));
f4.Write(&ihd, sizeof(BITMAPINFOHEADER));
f4.Write(&rgb, sizeof(rgb));
f4.Write(pdat2, glen);
f4.Close();
}


free(pdat2);
pdat2 = NULL;
//} //make gray bmp, 8bits;;


if(pDat!=NULL){
free(pDat);
pDat = NULL;
}


AfxMessageBox("OK");
}catch(CException *e){
CString sErr;
sErr.Format("Read Err\nerr code=%d", GetLastError());
//e->GetErrorMessage(sErr);
AfxMessageBox(sErr);
}
}else{
AfxMessageBox("Open file fail");
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,我们可以使用 Bitmap 类来读写位图。具体的操作步骤如下: 1. 从资源中读取位图 我们可以通过以下代码从资源中读取位图: ```java Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image); ``` 2. 从文件中读取位图 我们可以通过以下代码从文件中读取位图: ```java Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/my_image.jpg"); ``` 3. 从网络中读取位图 我们可以使用 Android 提供的网络请求库(如 Volley、OkHttp 等)从网络中读取位图: ```java ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { // 处理位图 } }, 0, 0, null, null); ``` 4. 将位图保存到文件 我们可以使用以下代码位图保存到文件: ```java File file = new File("/sdcard/my_image.jpg"); OutputStream os = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.flush(); os.close(); ``` 5. 从矢量绘图获取位图 我们可以使用以下代码从矢量绘图获取位图: ```java VectorDrawable vectorDrawable = (VectorDrawable) getResources().getDrawable(R.drawable.my_vector); Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); ``` 以上就是关于 Android 中读写位图操作方法,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值