我正在使用我的Android相机进行一些图像处理。它采用NV21格式的原始数据,然后将其转换为RGB格式,以获得每帧的平均R值。帧速率设置为16,具有最佳的手机分辨率。有很多转换算法,而我使用的算法效果很好。然而,我坚持的问题是我想绘制一个R值对时间的图形,因为我在onPreviewFrame中调用转换,我无法弄清楚如何从这个方法获取R的值我可以执行图处理的主要活动。
我的相机类的onPreviewMethod是:
@Override
public void onPreviewFrame(byte[] data, Camera camera){
//check if data is null
if (data == null)
throw new NullPointerException();
Camera.Size size = camera.getParameters().getPreviewSize();
//check if size is null
if(size == null)
throw new NullPointerException();
//set resolution of camera view to optimal setting
int width = size.width;
int height = size.height;
Log.d("Resolution ", " "+String.valueOf(width)+" "+String.valueOf(height));
//call ImageProcess on the data to decode YUV420SP to RGB
imgAvg = ImageProcessing.decodeYUV420SPtoRedAvg(data, width, height);
imageIntensity = imgAvg/255;
//set value of Y on the text view
TextView valueOfY = (TextView)getRootView().findViewById(R.id.valueY);
valueY = imgAvg;
valueOfY.setText(Double.toString(imgAvg));
}
在这里 imgAvg 变量存储每个帧的平均R值。日志显示正确的结果,每秒有16个结果。我想从我正在绘制图表的主要活动中访问此数据。这样做的正确方法是什么?我可以以某种方式直接访问这些数据,或者我是否需要以某种形式存储数据然后从另一个活动访问它。 (N.B我想避免保存整个视频并进行图像处理。)谢谢。
答案 0 :(得分:1)
示例:
public class Camera {
public interface PreviewReadyCallback {
void onPreviewFrame(String value1, int value2, Double value3, Float value4, Bitmap value5); // Any value you want to get
}
PreviewReadyCallback mPreviewReadyCallback = null;
public void setOnPreviewReady(PreviewReadyCallback cb) {
mPreviewReadyCallback = cb;
}
@Override
public void onPreviewFrame(byte[] data, Camera camera){
//check if data is null
if (data == null)
throw new NullPointerException();
Camera.Size size = camera.getParameters().getPreviewSize();
//check if size is null
if(size == null)
throw new NullPointerException();
//set resolution of camera view to optimal setting
int width = size.width;
int height = size.height;
Log.d("Resolution ", " "+String.valueOf(width)+" "+String.valueOf(height));
//call ImageProcess on the data to decode YUV420SP to RGB
imgAvg = ImageProcessing.decodeYUV420SPtoRedAvg(data, width, height);
imageIntensity = imgAvg/255;
//set value of Y on the text view
TextView valueOfY = (TextView)getRootView().findViewById(R.id.valueY);
valueY = imgAvg;
valueOfY.setText(Double.toString(imgAvg));
mPreviewReadyCallback.onPreviewFrame(value1, value2, value3, value4, value5);
}
}
在YourActivity中
public class YourActivity extends Activity implements PreviewReadyCallback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Camera camera = new Camera();
camera.setOnPreviewReady(this);
}
@Override
public void onPreviewFrame(String value1, int value2, Double value3, Float value4, Bitmap value5) {
// Code here!
}
}