Я могу делать снимки автоматически, используя приведенный ниже фрагмент кода, но я понял, что процесс съемки занимает некоторое время, что приводит к ANR и сбою приложения.
Итак, чтобы преодолеть этопроблема, я знаю, что я должен использовать фоновый поток или Asynctask, но не могу понять, какая часть фрагмента должна быть в run () потока.Как заставить приведенный ниже фрагмент кода работать в фоновом потоке?Я новичок в многопоточности.
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
surfaceTexture = new SurfaceTexture(0);
}
public void takePictire() {
Camera cam = openFrontCamera(mContext);
if (cam != null) {
try {
cam.setPreviewTexture(surfaceTexture);
cam.startPreview();
cam.takePicture(null, null, mPicture);
} catch (Exception ex) {
Log.d(LOG_TAG, "Can't take picture!");
}
}
}
private static Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(new
ByteArrayInputStream(data), null, bfo);
// Eye distance detection here and saving data
camera.stopPreview();
camera.release();
}
};
/* Check if this device has a camera */
private static Camera openFrontCamera(Context context) {
try {
boolean hasCamera =
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
if (hasCamera) {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing ==
Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(LOG_TAG, "Camera failed to open: " +
e.getLocalizedMessage());
}
}
}
return cam;
}
} catch (Exception ex) {
Log.d(LOG_TAG, "Can't open front camera");
}
return null;
}
После выполнения снимков поток пользовательского интерфейса перестает отвечать на запросы.
2019-09-29 17:14:23.235 1555-1555/com.example.devyankshaw.checking W/InputMethodManager: startInputReason = 7
2019-09-29 17:14:23.259 1555-1555/com.example.devyankshaw.checking D/ZrHung.AppEyeUiProbe: notify runnable to start.
2019-09-29 17:14:23.260 1555-1706/com.example.devyankshaw.checking D/ZrHung.AppEyeUiProbe: restart watching
2019-09-29 17:14:23.335 1555-1555/com.example.devyankshaw.checking I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
2019-09-29 17:14:23.520 1555-1709/com.example.devyankshaw.checking W/libEGL: EGLNativeWindowType 0x7e75a9a010 disconnect failed
2019-09-29 17:14:23.521 1555-1709/com.example.devyankshaw.checking D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-09-29 17:14:23.618 1555-1709/com.example.devyankshaw.checking W/libEGL: EGLNativeWindowType 0x7e75a9a010 disconnect failed
2019-09-29 17:14:23.619 1555-1709/com.example.devyankshaw.checking D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-09-29 17:14:27.221 1555-1555/com.example.devyankshaw.checking W/HiTouch_PressGestureDetector: Touch pointer move a lot. The moving distance of X is:84.145966, limit is:60The moving distance of Y is:94.54834, limit is:60
2019-09-29 17:14:27.885 1555-1555/com.example.devyankshaw.checking I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
2019-09-29 17:14:27.895 1555-1709/com.example.devyankshaw.checking D/OpenGLRenderer: HWUI Binary is enabled
2019-09-29 17:14:27.943 1555-1709/com.example.devyankshaw.checking D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-09-29 17:14:27.949 1555-1709/com.example.devyankshaw.checking D/OpenGLRenderer: HWUI Binary is enabled
2019-09-29 17:14:29.386 1555-1555/com.example.devyankshaw.checking W/HiTouch_PressGestureDetector: Touch pointer move a lot. The moving distance of X is:64.3154, limit is:60The moving distance of Y is:74.54968, limit is:60
2019-09-29 17:14:29.894 1555-1709/com.example.devyankshaw.checking W/libEGL: EGLNativeWindowType 0x7e72bf1010 disconnect failed
2019-09-29 17:14:30.827 1555-1709/com.example.devyankshaw.checking D/OpenGLRenderer: HWUI Binary is enabled
2019-09-29 17:14:30.875 1555-1709/com.example.devyankshaw.checking D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-09-29 17:14:30.884 1555-1709/com.example.devyankshaw.checking D/OpenGLRenderer: HWUI Binary is enabled
2019-09-29 17:14:32.667 1555-1555/com.example.devyankshaw.checking W/HiTouch_PressGestureDetector: Touch pointer move a lot. The moving distance of X is:63.654022, limit is:60The moving distance of Y is:74.48108, limit is:60
2019-09-29 17:14:32.824 1555-1709/com.example.devyankshaw.checking W/libEGL: EGLNativeWindowType 0x7e72bf1010 disconnect failed
2019-09-29 17:14:35.178 1555-1555/com.example.devyankshaw.checking I/PermissionManager: camera remind result:true
2019-09-29 17:14:35.178 1555-1555/com.example.devyankshaw.checking I/Camera: open camera: 1, package name: com.example.devyankshaw.checking
2019-09-29 17:14:35.178 1555-1555/com.example.devyankshaw.checking I/BackgroundPermManager: pkgName: com.example.devyankshaw.checking,pid: 1555 ,uidOf3RdApk: 10270 ,permType: 0 ,permCfg: 1
2019-09-29 17:14:35.182 1555-1555/com.example.devyankshaw.checking I/HwCameraUtil: notifySurfaceFlingerCameraStatus : isFront = true , isOpend = true
2019-09-29 17:14:35.183 1555-1555/com.example.devyankshaw.checking I/HwCameraUtil: notifySurfaceFlingerFrontCameraStatus 8011 transact success!
2019-09-29 17:14:35.206 1555-1555/com.example.devyankshaw.checking D/ActivityThread: isOptEnabled=false isWechatScanOpt=false
2019-09-29 17:14:35.283 1555-1709/com.example.devyankshaw.checking D/OpenGLRenderer: HWUI Binary is enabled
2019-09-29 17:14:35.299 1555-1709/com.example.devyankshaw.checking D/OpenGLRenderer: HWUI Binary is enabled
2019-09-29 17:14:35.336 1555-1709/com.example.devyankshaw.checking D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-09-29 17:14:35.421 1555-1709/com.example.devyankshaw.checking W/libEGL: EGLNativeWindowType 0x7e72bf1010 disconnect failed
2019-09-29 17:14:35.423 1555-1709/com.example.devyankshaw.checking D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-09-29 17:14:40.271 1555-1706/com.example.devyankshaw.checking E/ZrHungImpl: sendAppFreezeEvent failed!
2019-09-29 17:14:42.576 1555-1706/com.example.devyankshaw.checking E/ZrHungImpl: sendAppFreezeEvent failed!
2019-09-29 17:14:45.373 1555-1555/com.example.devyankshaw.checking D/Image Log:: iVBORw0KGgoAAAANSUhEUgAADYAAABIACAIAAABpOSKkAAAAA3NCSVQFBgUzC42AAAAgAElEQVR4
nOy9u7btIK8mqHMGAc5O0EEFHXS9/wOagKADcxFCEuJir7n/qm+ssfecnjYIIYQQQv6v//n//jcs
IOIvd/3oyqeQ7nFghK+Fe/muCwACJaAryuUSxNsCqjY0vzgoLWp/AIAATqENwIFvawxArgRMVcit
xtQgjqG6HFzp00VYeoPQzHzxcuXzzd3XVNY1GdXSIJOjdoT09NVLRbrS1R/hjhJRAAAhAgD4UqDe
QQ3aOx1AalXLYbmBtFURXXt4jstpm5ya5Fo2uny9qxSzwMdac2mDd1etPebucV1R7qKENQ25CwGZ
wkZiKRyl1jdEUbQ/XOgzL5kXe5UMk645GQEAwHmIAXql4eAuX7GmUpXP1deY7/euDudSGie46BpD
tq/0YNpKwTHcvcpqCiQ/3VUScisAjZcAvpOQ/CRXQ+gGI+LX/TyOe80P9EMArK47Gpr7MCIaLHA1
xOYa73zbFTs5iugBhtsAzl8dBxit1ZPJjCxN6wZ0/0hzoVYgmi+Ow22nXKVreDgIsU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ZtGma7JeTAv
IqqFcziU7sYkabSlGhvDLwLi940uVlht5kHtLRH9s/mzVp0TSkD4/hyyMcQqxN6trbXVwHbhFmNq
n0Ya5XI6b7Y3ro961V3t0jyUpAcNCJKJctZPcnJSlli9JbMTD09xhmwuAwxEkguPC6Ckrh+DSX8I
yz2CiP/Yr7gvQs689MNXHABNAc7dH/m+G+Bt9+z77l+yremL8wQ
2019-09-29 17:14:45.550 1555-1555/com.example.devyankshaw.checking I/Camera: close camera: 1, package name: com.example.devyankshaw.checking
2019-09-29 17:14:45.550 1555-1555/com.example.devyankshaw.checking I/BackgroundPermManager: pkgName: com.example.devyankshaw.checking,pid: 1555 ,uidOf3RdApk: 10270 ,permType: 0 ,permCfg: 0
2019-09-29 17:14:45.554 1555-1555/com.example.devyankshaw.checking I/HwCameraUtil: notifySurfaceFlingerCameraStatus : isFront = true , isOpend = false
2019-09-29 17:14:45.556 1555-1555/com.example.devyankshaw.checking I/HwCameraUtil: notifySurfaceFlingerFrontCameraStatus 8012 transact success!
2019-09-29 17:14:45.635 1555-1709/com.example.devyankshaw.checking W/libEGL: EGLNativeWindowType 0x7e72bf1010 disconnect failed
2019-09-29 17:14:45.878 1555-1706/com.example.devyankshaw.checking D/ZrHung.AppEyeUiProbe: sendAppEye recover Events.
2019-09-29 17:14:45.882 1555-1706/com.example.devyankshaw.checking E/ZrHungImpl: sendAppFreezeEvent failed!