package com.scy.grayvalue2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
/**
* 思路如下:
1.读取or照相,得到一张ARGB图片。
2.转化为bitmap类,并对其数据做如下操作:
A通道保持不变,然后逐像素计算:X = 0.3×R+0.59×G+0.11×B,并使这个像素的值新R,G,B值为X,即:
new_R = X, new_G = X, new_B = X
例如:原来一个像素是4个byte,分别为ARGB,现在这个像素应该为AXXX。
3.将上一步骤得到的bitmap图像写到输出流里面,并保存为图片。或者直接显示在ImageView上。
* @author scy
*
*/
public class MainActivity extends Activity
{
private byte[] rawData;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.image);
//将bitmap转换成一个伪灰度图,再转化成一个byte[]
//将byte[]转换为bitmap
// Bitmap bitmapOrg = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap bitmapNew = bitmapOrg.copy(Config.ARGB_8888, true);
//Bitmap bitmapNew = bitmapOrg.copy(Config.ARGB_8888, true);
if(bitmapNew == null)
Log.i("TAG", "null");
Log.i("TAG", "copy end");
/**
* 一种简单的灰度化算法,但是这种方法需要修改每个点的像素,如果一张图片比较大,则这个处理速度就堪忧了
* 往往需要创建一个线程来处理
*/
for(int i = 0;i<bitmapNew.getWidth();i++)
{
for(int j =0;j<bitmapNew.getHeight();j++)
{
int col = bitmapNew.getPixel(i, j);//获取点(i,j)处的像素
//分别获取ARGB的值
int alpha = col&0xFF000000;
int red = (col&0x00FF0000)>>16;
int green = (col&0x0000FF00)>>8;
int blue = (col&0x000000FF);
//用公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB
int gray = (int)((float)red*0.3+(float)green*0.59+(float)blue*0.11);
//新的ARGB
int newColor = alpha|(gray<<16)|(gray<<8)|gray;
//在点(i,j)处设置新的像素
bitmapNew.setPixel(i, j, newColor);
//Log.v("tag", Integer.toHexString(col));
}
}
Log.i("TAG", "pro end");
sendMsg(bitmapNew);
/*
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"gray"+number+".jpg");
OutputStream out;
try {
out = new FileOutputStream(file);
if(bitmapNew.compress(Bitmap.CompressFormat.JPEG, 100, out))
Log.i("TAG", "success");
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
}
private void sendMsg(Bitmap bitmapNew)
{
imageView.setImageBitmap(bitmapNew);
}
}
/**
* drawableToBitmap
*/
/*
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
*/
/**
* Bitmap2Bytes
*/
/*
private byte[] Bitmap2Bytes(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
*/
/**
* Bytes2Bimap
*/
/*
private Bitmap Bytes2Bimap(byte[] b)
{
if(b.length ==0)
{
return null;
}
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
*/
package com.scy.grayvalue2;
import java.io.Fil