只是Python中的一个简单代码:
public class CameraExample extends AnimatedViewContainer {
private final static String TAG = "CameraExample";
private Camera mCamera;
private CameraPreview frontPreview, backPreview;
private Context mContext;
public int camIter = 1;
public CameraExample(Context context, int i) {
super(context, i);
frontPreview = null;
backPreview = null;
mContext = context;
initCamera(context);
}
private void initCamera(Context context) {
// Check if this device has a camera
if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// No camera on this device
Log.d("Camera", "CameraExample: " + "this device has no camera");
} else {
// This device has a camera
int numCameras = Camera.getNumberOfCameras();
if (numCameras >= 1) {
switch (camIter) {
case 1:
for (int cameraId = 1; cameraId < numCameras; cameraId++) {
mCamera = getCameraInstance(cameraId);
if (mCamera != null) {
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
//Create our Preview view and set it as the content of this LinearLayout View
frontPreview = new CameraPreview(context, mCamera, cameraId);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
if (createView() == false) {
break;
}
}
}
break;
case 2:
for (int cameraId = 0; cameraId < 1; cameraId++) {
mCamera = getCameraInstance(cameraId);
if (mCamera != null) {
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
try {
//Create our Preview view and set it as the content of this LinearLayout View
backPreview = new CameraPreview(context, mCamera, cameraId);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
if (createView() == false) {
break;
}
}
}
break;
}
}
}
}
public static Camera getCameraInstance(int cameraId) {
Camera c = null;
try {
// attempt to get a Camera instance
c = Camera.open(cameraId);
} catch (Exception e) {
// Camera is not available (in use or does not exist)
Log.d(TAG, "CameraExample: " + "camera not available (in use or does not exist); " + e.getMessage());
}
// returns null if camera is unavailable
return c;
}
@Override
public void onCreateViewContent(LayoutInflater layoutInflater, ViewGroup parentGroup, View[] containerViews, int index) {
containerViews[index] = layoutInflater.inflate(R.layout.example_camera, parentGroup, false);
FrameLayout previewFrame = (FrameLayout) containerViews[index].findViewById(R.id.preview);
// set camera preview
if (camIter == 1) {
previewFrame.addView(frontPreview);
}
if (camIter == 2) {
previewFrame.addView(backPreview);
}
}
@Override
public void cleanup() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
// A basic Camera preview class
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private Context mContext;
private SurfaceHolder mHolder;
private Camera mCamera;
private int mCameraId;
public CameraPreview(Context context, Camera camera, int cameraId) {
super(context);
mContext = context;
mCamera = camera;
mCameraId = cameraId;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
// infinite for loop - doesn't work
if(camIter == 1){
cleanup();
initCamera(getContext());
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
camIter = 2;
// Thread.sleep(2000); - Doesn't work!
}
if(camIter == 2){
cleanup();
initCamera(getContext());
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
camIter = 1;
// Thread.sleep(2000); - Doesn't work!
}
} catch (IOException e) {
Log.d(TAG, "CameraExample: " + "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
}
}
chromedriver:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--user-agent={}".format(config.USER_AGENT))
driver = webdriver.Chrome("/usr/local/bin/chromedriver",
chrome_options=chrome_options)
和
$ ls /usr/local/bin/chromedriver
/usr/local/bin/chromedriver
但是当我运行python脚本时,它会抛出一个异常,它无法连接到chromedriver:
$ chromedriver
Starting ChromeDriver 2.29 on port 9515
Only local connections are allowed.
如何解决?
答案 0 :(得分:0)
以下是您的问题的答案:
我在运行Python 3.x的Windows 8 Pro机器上对您的代码进行了简单的检查,并进行了两处小改动。首先,我已将参数类型executable_path
添加到chromedriver的绝对路径中,并且它在我的结尾处工作正常。第二,直到&amp;除非我们调用并使用chromedriver服务,否则我们并不需要通过$ chromedriver
启动chromedriver,并且可以通过我们的代码继续引用我们机器中chromedriver
的绝对路径。以下是您自己的代码块将打开Google Chrome v59.0:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--user-agent={}".format(config.USER_AGENT))
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver",
chrome_options=chrome_options)
如果这回答你的问题,请告诉我。