public abstract class ImageProcessing {
/**
* 内部调用的处理图片的方法
* @param yuv420sp
* @param width
* @param height
* @return
*/
private static int decodeYUV420SPtoRedSum(byte[] yuv420sp, int width,int height) {
if (yuv420sp == null)
return 0;
final int frameSize = width * height;
int sum = 0;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
int pixel = 0xff000000 | ((r << 6) & 0xff0000)
| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
int red = (pixel >> 16) & 0xff;
sum += red;
}
}
return sum;
}
/**
* 对外开放的图像处理方法
* @param yuv420sp
* @param width
* @param height
* @return
*/
public static int decodeYUV420SPtoRedAvg(byte[] yuv420sp, int width,
int height) {
if (yuv420sp == null)
return 0;
final int frameSize = width * height;
int sum = decodeYUV420SPtoRedSum(yuv420sp, width, height);
return (sum / frameSize);//返回每个点的像素值
}
}
private static PreviewCallback previewCallback = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera cam) {
if (data == null)
throw new NullPointerException();
Camera.Size size = cam.getParameters().getPreviewSize();
if (size == null)
throw new NullPointerException();
if (!processing.compareAndSet(false, true))
return;
int width = size.width;
int height = size.height;
//图像处理
int imgAvg = ImageProcessing.decodeYUV420SPtoRedAvg(data.clone(),height,width);
gx=imgAvg;
text1.setText("平均像素值是"+String.valueOf(imgAvg));
//像素平均值imgAvg,日志
//Log.i(TAG, "imgAvg=" + imgAvg);
if (imgAvg == 0 || imgAvg == 255) {
processing.set(false);
return;
}
//计算平均值
int averageArrayAvg = 0;
int averageArrayCnt = 0;
for (int i = 0; i < averageArray.length; i++) {
if (averageArray[i] > 0) {
averageArrayAvg += averageArray[i];
averageArrayCnt++;
}
}
//计算平均值
int rollingAverage = (averageArrayCnt > 0)?(averageArrayAvg/averageArrayCnt):0;
TYPE newType = currentType;
if (imgAvg < rollingAverage) {
newType = TYPE.RED;
if (newType != currentType) {
beats++;
flag=0;
text2.setText("脉冲数是"+String.valueOf(beats));
//Log.e(TAG, "BEAT!! beats=" + beats);
}
} else if (imgAvg > rollingAverage) {
newType = TYPE.GREEN;
}
if (averageIndex == averageArraySize)
averageIndex = 0;
averageArray[averageIndex] = imgAvg;
averageIndex++;
// Transitioned from one state to another to the same
if (newType != currentType) {
currentType = newType;
//image.postInvalidate();
}
//获取系统结束时间(ms)
long endTime = System.currentTimeMillis();
double totalTimeInSecs = (endTime - startTime) / 1000d;
if (totalTimeInSecs >= 2) {
double bps = (beats / totalTimeInSecs);
int dpm = (int) (bps * 60d);
if (dpm < 30 || dpm > 180||imgAvg<200) {
//获取系统开始时间(ms)
startTime = System.currentTimeMillis();
//beats心跳总数
beats = 0;
processing.set(false);
return;
}
//Log.e(TAG, "totalTimeInSecs=" + totalTimeInSecs + " beats="+ beats);
if (beatsIndex == beatsArraySize)
beatsIndex = 0;
beatsArray[beatsIndex] = dpm;
beatsIndex++;
int beatsArrayAvg = 0;
int beatsArrayCnt = 0;
for (int i = 0; i < beatsArray.length; i++) {
if (beatsArray[i] > 0) {
beatsArrayAvg += beatsArray[i];
beatsArrayCnt++;
}
}
int beatsAvg = (beatsArrayAvg / beatsArrayCnt);
text.setText("您的的心率是"+String.valueOf(beatsAvg)+" zhi:"+String.valueOf(beatsArray.length)
+" "+String.valueOf(beatsIndex)+" "+String.valueOf(beatsArrayAvg)+" "+String.valueOf(beatsArrayCnt));
//获取系统时间(ms)
startTime = System.currentTimeMillis();
beats = 0;
}
processing.set(false);
}
};
/**
* 预览回调接口
*/
网友评论