gpt4 book ai didi

android - Camera.open() 阻塞 UI 线程

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:30 25 4
gpt4 key购买 nike

我查看了我能找到的关于此的所有 SO 文章,但没有一个解决方案适合我。

当调用 Camera.open() 时,UI 线程被阻塞时会有 3 秒(或多或少)的延迟。我试图把它放在后台线程中。我目前正在使用找到的解决方案 here (粘贴在下方)但是“等待”方法是同步的,因此它也会阻塞 UI 线程。

我想要做的是加载这个 fragment ,显示一个进度微调器,直到相机准备就绪,然后在屏幕上显示相机,但这种延迟让我很难受,我似乎找不到任何真正的东西很好的解决方案。

我的 fragment :

public class BarcodeFinderFragment extends Fragment implements View.OnClickListener, Camera.AutoFocusCallback, Camera.PreviewCallback {

private static final String CAMERA_THREAD_NAME = "CAMERA_THREAD_NAME";
private Camera mCamera;

private CamViewFinder mPreview;
private Handler autoFocusHandler;
private boolean previewing = true;
private Button noScan;
private Button noBarcode;
private FrameLayout preview;
private BarcodeFinderCallback callBack;
private ImageScanner scanner;

private CameraHandlerThread mThread = null;


private BarcodeFinderCallback dummyCallback = new BarcodeFinderCallback() {
@Override
public void onNoScanClicked() {

}

@Override
public void onNoBarcodeClicked() {

}

@Override
public void finishActivity() {

}

@Override
public void setActivityResult(Bundle bundle) {

}

@Override
public void showProgressDialog(boolean showProgress) {

}
};

public static BarcodeFinderFragment newInstance() {
return new BarcodeFinderFragment();
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

try {
callBack = (BarcodeFinderActivity) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_barcode_finder, container, false);

noScan = (Button) view.findViewById(R.id.btnNoScan);
noBarcode = (Button) view.findViewById(R.id.btnNobarcode);
preview = (FrameLayout) view.findViewById(R.id.cameraPreview);
noScan.setOnClickListener(this);
noBarcode.setOnClickListener(this);

return view;
}

@Override
public void onResume() {
super.onResume();

autoFocusHandler = new Handler();

//Instance barcode scanner
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);

openCamera();

mPreview = new CamViewFinder(getActivity(), mCamera, BarcodeFinderFragment.this, BarcodeFinderFragment.this);
preview.addView(mPreview);
callBack.showProgressDialog(false);
}

private void getCamera() {
mCamera = null;
try {
mCamera = Camera.open();
} catch (final Exception e) {
Log.d("BarcodeFinderFragment", e.toString());
}
}

private void openCamera() {
if (mThread == null)
mThread = new CameraHandlerThread(CAMERA_THREAD_NAME);

synchronized (mThread) {
mThread.openCamera();
}
}

@Override
public void onPause() {
super.onPause();
releaseCamera();
}

@Override
public void onDetach() {
super.onDetach();

callBack = dummyCallback;
}

private Runnable doAutoFocus() {
return new Runnable() {
@Override
public void run() {
if (previewing) {
mCamera.autoFocus(BarcodeFinderFragment.this);
}
}
};
}

private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}

callBack.finishActivity();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNoScan:
callBack.onNoScanClicked();
break;

case R.id.btnNobarcode:
callBack.onNoBarcodeClicked();
break;
}
}

@Override
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus(), 1000);
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {

final Camera.Parameters parameters = camera.getParameters();
final Camera.Size size = parameters.getPreviewSize();

final Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);

final int result = scanner.scanImage(barcode);

if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();

final SymbolSet syms = scanner.getResults();
for (final Symbol sym : syms) {
final Bundle bundle = new Bundle();
bundle.putString("result", sym.getData());
bundle.putString("codeType", "" + sym.getType());

callBack.setActivityResult(bundle);
}
}
}

public interface BarcodeFinderCallback {
void onNoScanClicked();

void onNoBarcodeClicked();

void finishActivity();

void setActivityResult(Bundle bundle);

void showProgressDialog(boolean showProgress);
}

private class CameraHandlerThread extends HandlerThread {

Handler mHandler = null;

public CameraHandlerThread(String name) {
super(name);
callBack.showProgressDialog(true);
start();

mHandler = new Handler(getLooper());
}

synchronized void notifyCameraOpened() {
notify();
}

void openCamera() {
mHandler.post(new Runnable() {
@Override
public void run() {
getCamera();
notifyCameraOpened();
}
});

try {
wait();
} catch (InterruptedException e) {
Log.d("BarcodeFinderFragment", "wait was interrupted");
}
}
}
}

更新

感谢 MeetTitan,通过将所有内容保留在后台线程中并在需要时发布到 UI,我能够非常顺利地完成这项工作。这是供将来可能需要它的任何人使用的工作代码:)

public class BarcodeFinderFragment extends Fragment implements View.OnClickListener {

private static final String CAMERA_THREAD_NAME = "CAMERA_THREAD_NAME";
private Camera mCamera;
private CamViewFinder mPreview;
private Handler autoFocusHandler;
private FrameLayout preview;
private ImageScanner scanner;
private boolean previewing = true;
private CameraHandlerThread mThread = null;

private BarcodeFinderCallback callBack;
private BarcodeFinderCallback dummyCallback = new BarcodeFinderCallback() {
@Override
public void onNoScanClicked() {
}

@Override
public void onNoBarcodeClicked() {
}

@Override
public void finishActivity() {
}

@Override
public void setActivityResult(int resultCode, Bundle bundle) {
}

@Override
public void showProgressDialog(boolean showProgress) {
}
};

public static BarcodeFinderFragment newInstance() {
return new BarcodeFinderFragment();
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

try {
callBack = (BarcodeFinderActivity) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement BarcodeFinderCallback");
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_barcode_finder, container, false);

Button noScan = (Button) view.findViewById(R.id.btnNoScan);
Button noBarcode = (Button) view.findViewById(R.id.btnNobarcode);
preview = (FrameLayout) view.findViewById(R.id.cameraPreview);
noScan.setOnClickListener(this);
noBarcode.setOnClickListener(this);

return view;
}

@Override
public void onResume() {
super.onResume();

autoFocusHandler = new Handler();

//Instance barcode scanner
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);

callBack.showProgressDialog(true);
openCamera();

}

private void openCamera() {
if (mThread == null) {
try {
mThread = new CameraHandlerThread(CAMERA_THREAD_NAME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (mThread) {
mThread.openCamera();
}
}

@Override
public void onPause() {
super.onPause();
releaseCamera();

if (mThread != null && mThread.isAlive())
mThread.interrupt();
}

@Override
public void onDetach() {
super.onDetach();

callBack = dummyCallback;
}

private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}

callBack.finishActivity();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNoScan:
callBack.onNoScanClicked();
break;

case R.id.btnNobarcode:
callBack.onNoBarcodeClicked();
break;
}
}

public interface BarcodeFinderCallback {
void onNoScanClicked();

void onNoBarcodeClicked();

void finishActivity();

void setActivityResult(int resultCode, Bundle bundle);

void showProgressDialog(boolean showProgress);
}

private class CameraHandlerThread extends HandlerThread implements Camera.AutoFocusCallback, Camera.PreviewCallback {

Handler mHandler = null;

public CameraHandlerThread(String name) throws InterruptedException {
super(name);
callBack.showProgressDialog(true);
start();

mHandler = new Handler(getLooper());
}

void openCamera() {
mHandler.post(new Runnable() {
@Override
public void run() {
mCamera = null;
try {
mCamera = Camera.open();
} catch (final Exception e) {
Log.d("BarcodeFinderFragment", e.toString());
callBack.setActivityResult(Activity.RESULT_CANCELED, null);
interrupt();
}
notifyCameraOpened();

getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mPreview = new CamViewFinder(getActivity(), mCamera, CameraHandlerThread.this, CameraHandlerThread.this);
preview.addView(mPreview);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
callBack.showProgressDialog(false);
}
}, 500);
}
});
}
});
}

synchronized void notifyCameraOpened() {
notify();

}

@Override
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (previewing) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mCamera.autoFocus(CameraHandlerThread.this);
}
});
}
}
}, 1000);
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {

final Camera.Parameters parameters = camera.getParameters();
final Camera.Size size = parameters.getPreviewSize();

final Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);

final int result = scanner.scanImage(barcode);

if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();

final SymbolSet syms = scanner.getResults();
for (final Symbol sym : syms) {
final Bundle bundle = new Bundle();
bundle.putString("result", sym.getData());
bundle.putString("codeType", "" + sym.getType());

getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
callBack.setActivityResult(Activity.RESULT_OK, bundle);
}
});
}
}
}
}
}

最佳答案

你不能继续你的线程并调用 ui 命令组和 yourContext.runOnUiThread() 方法吗?然后你后台任何阻塞代码,等待相机准备就绪,并从后台线程更新用户界面。

例如:

private class CameraHandlerThread extends ... {
public void run() {
getCamera();
yourContext.runOnUiThread(new Runnable(){
public void run()
{
...
}
});
}
}

然后你可以简单地new CameraHandlerThread().start();

关于android - Camera.open() 阻塞 UI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27869530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com