作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用OpenCV教程3代码(摄像机控制)(请参阅OpenCV4Android samples)作为我的代码的基础,并且一旦将图像保存在onPictureTaken()
方法中,就需要能够打开和操作该图像。在操作期间,我希望能够显示图像的更改并使用Toast
发布消息,这两者都要求我位于主线程中。
根据迄今为止收到的建议,我正在尝试在BroadcastReceiver
中使用Activity
,这是由sendBroadcast()
中的JavaCameraView
触发的。
我已经将Activity
更新为包括BroadcastReceiver
,并按如下所示进行注册和注销:
public class Tutorial3Activity extends Activity implements CvCameraViewListener2, OnTouchListener {
:
private ActivityReceiver activityReceiver;
:
@Override
public void onCreate(Bundle savedInstanceState) {
:
IntentFilter intentFilter = new IntentFilter();
this.registerReceiver(activityReceiver, intentFilter);
:
}
:
public void onDestroy() {
:
this.unregisterReceiver(activityReceiver);
:
}
:
private class ActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BroadcastReceiver triggered");
if (intent.getAction().equals(Tutorial3View.IMAGE_READY)) {
Log.i(TAG, "BroadcastReceiver received");
}
}
}
}
sendBroadcast()
在
JavaCameraView
中被调用,如下所示:
public class Tutorial3View extends JavaCameraView {
:
public static final String IMAGE_READY = "IMAGE_READY";
private Context context;
:
public Tutorial3View(Context context, AttributeSet attrs) {
:
this.context = context;
:
}
:
public void takePicture(final String fileName) {
PictureCallback callback = new PictureCallback() {
private String mPictureFileName = fileName;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
:
image_read();
:
}
};
mCamera.takePicture(null, null, callback);
}
:
public void image_read() {
Log.i(TAG, "BroadcastReceiver - send");
Intent i = new Intent();
i.setAction(IMAGE_READY);
context.sendBroadcast(i);
Log.i(TAG, "BroadcastReceiver - sent");
}
}
Manifest
包含
BroadcastReceiver
,如下所示:
<receiver android:name="ActivityReceiver">
<intent-filter android:label="IMAGE_READY"></intent-filter>
</receiver>
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - send
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - sent
BroadcastReceiver triggered
或
BroadcastReceiver received
的日志。
最佳答案
您需要调用 runOnUiThread()
方法:
runOnUiThread(new Runnable() {
@Override
public void run() {
// stuff to be done on UI Thread
}
});
关于android - 使用Android,如何将控制权传递回主线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15718145/
我是一名优秀的程序员,十分优秀!