Java Code Examples for android.hardware.Camera#setPreviewCallback()

The following examples show how to use android.hardware.Camera#setPreviewCallback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraTester.java    From PermissionAgent with Apache License 2.0 6 votes vote down vote up
@Override
public boolean test() throws Throwable {
    Camera camera = null;
    try {
        camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        camera.setParameters(parameters);
        camera.setPreviewCallback(PREVIEW_CALLBACK);
        camera.startPreview();
        return true;
    } catch (Throwable e) {
        PackageManager packageManager = mContext.getPackageManager();
        return !packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
    } finally {
        if (camera != null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
        }
    }
}
 
Example 2
Source File: CameraManager.java    From Android with MIT License 6 votes vote down vote up
/**
 * Set the camera to camera (turn off the old camera, turn on the new camera)
 *
 * @param mCamera
 */
public Camera setChangeParameters(Activity activity, Camera mCamera, SurfaceHolder viewHolder) {
    if (Camera.getNumberOfCameras() <= 1) {
        return null;
    }
    try {
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
        mCamera.release();
        cameraPosition = cameraPosition == 1 ? 0 : 1;
        Camera mNewCamera = getCameraInstance(cameraPosition);
        setParametersCamera(activity, mNewCamera);
        mNewCamera.setPreviewDisplay(viewHolder);
        mNewCamera.startPreview();
        return mNewCamera;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: CodeScanner.java    From code-scanner with MIT License 6 votes vote down vote up
private void stopPreviewInternal(final boolean internal) {
    try {
        final DecoderWrapper decoderWrapper = mDecoderWrapper;
        if (decoderWrapper != null) {
            final Camera camera = decoderWrapper.getCamera();
            camera.cancelAutoFocus();
            final Parameters parameters = camera.getParameters();
            if (!internal && decoderWrapper.isFlashSupported() && mFlashEnabled) {
                Utils.setFlashMode(parameters, Parameters.FLASH_MODE_OFF);
            }
            camera.setParameters(parameters);
            camera.setPreviewCallback(null);
            camera.stopPreview();
        }
    } catch (final Exception ignored) {
    }
    mStoppingPreview = false;
    mPreviewActive = false;
    mSafeAutoFocusing = false;
    mSafeAutoFocusAttemptsCount = 0;
}
 
Example 4
Source File: PreviewCallback.java    From vmqApk with MIT License 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 5
Source File: PreviewCallback.java    From smart-farmer-android with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 6
Source File: PreviewCallback.java    From QrModule with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
    Point cameraResolution = configManager.getCameraResolution();
    if (!useOneShotPreviewCallback) {
        camera.setPreviewCallback(null);
    }
    if (previewHandler != null) {
        Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
                cameraResolution.y, data);
        message.sendToTarget();
        previewHandler = null;
    } else {
        Log.d(TAG, "Got preview callback, but no handler for it");
    }
}
 
Example 7
Source File: PreviewCallback.java    From Mobike with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
    Point cameraResolution = configManager.getCameraResolution();
    if (!useOneShotPreviewCallback) {
        camera.setPreviewCallback(null);
    }
    if (previewHandler != null) {
        Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
                cameraResolution.y, data);
        message.sendToTarget();
        previewHandler = null;
    } else {
        Log.d(TAG, "Got preview callback, but no handler for it");
    }
}
 
Example 8
Source File: PreviewCallback.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 9
Source File: CodeScanner.java    From code-scanner with MIT License 5 votes vote down vote up
private void startPreviewInternal(final boolean internal) {
    try {
        final DecoderWrapper decoderWrapper = mDecoderWrapper;
        if (decoderWrapper != null) {
            final Camera camera = decoderWrapper.getCamera();
            camera.setPreviewCallback(mPreviewCallback);
            camera.setPreviewDisplay(mSurfaceHolder);
            if (!internal && decoderWrapper.isFlashSupported() && mFlashEnabled) {
                setFlashEnabledInternal(true);
            }
            camera.startPreview();
            mStoppingPreview = false;
            mPreviewActive = true;
            mSafeAutoFocusing = false;
            mSafeAutoFocusAttemptsCount = 0;
            if (decoderWrapper.isAutoFocusSupported() && mAutoFocusEnabled) {
                final Rect frameRect = mScannerView.getFrameRect();
                if (frameRect != null) {
                    final Parameters parameters = camera.getParameters();
                    Utils.configureDefaultFocusArea(parameters, decoderWrapper, frameRect);
                    camera.setParameters(parameters);
                }
                if (mAutoFocusMode == AutoFocusMode.SAFE) {
                    scheduleSafeAutoFocusTask();
                }
            }
        }
    } catch (final Exception ignored) {
    }
}
 
Example 10
Source File: PreviewCallback.java    From KSYMediaPlayer_Android with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 11
Source File: PreviewCallback.java    From QrScan with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 12
Source File: PreviewCallback.java    From MaterialHome with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 13
Source File: PreviewCallback.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
    Point cameraResolution = configManager.getCameraResolution();
    if (!useOneShotPreviewCallback) {
        camera.setPreviewCallback(null);
    }
    if (previewHandler != null) {
        Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
                cameraResolution.y, data);
        message.sendToTarget();
        previewHandler = null;
    } else {
        Log.d(TAG, "Got preview callback, but no handler for it");
    }
}
 
Example 14
Source File: PreviewCallback.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 15
Source File: PreviewCallback.java    From iscanner_android with MIT License 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
  Point cameraResolution = configManager.getCameraResolution();
  if (!useOneShotPreviewCallback) {
    camera.setPreviewCallback(null);
  }
  if (previewHandler != null) {
    Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
        cameraResolution.y, data);
    message.sendToTarget();
    previewHandler = null;
  } else {
    Log.d(TAG, "Got preview callback, but no handler for it");
  }
}
 
Example 16
Source File: PreviewCallback.java    From QrCodeLib with MIT License 5 votes vote down vote up
public void onPreviewFrame(byte[] data, Camera camera) {
    Point cameraResolution = configManager.getCameraResolution();
    if (!useOneShotPreviewCallback) {
        camera.setPreviewCallback(null);
    }
    if (previewHandler != null) {
        Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
                cameraResolution.y, data);
        message.sendToTarget();
        previewHandler = null;
    } else {
        Log.d(TAG, "Got preview callback, but no handler for it");
    }
}
 
Example 17
Source File: CameraUtils5.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setCallback(VideoCaptureAndroid captureAndroid, int numCaptureBuffers, int bufSize,
        Camera camera) {

    camera.setPreviewCallback(captureAndroid);
}
 
Example 18
Source File: QrCodeDecoder.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
private void askForPreviewFrame(Camera camera) {
	if(!stopped) camera.setPreviewCallback(this);
}
 
Example 19
Source File: FaceTrackActivity.java    From FaceDetect with Apache License 2.0 4 votes vote down vote up
@Override
    public void onPreviewFrame(final byte[] data, final Camera camera) {
        camera.setPreviewCallback(null);
        if (mCamera == null) return;
        detectHandler.post(new Runnable() {
            @Override
            public void run() {
                int is = 0;
                final byte[] ori = new byte[width * height];
                for (int x = width - 1; x >= 0; x--) {
                    for (int y = height - 1; y >= 0; y--) {
                        ori[is++] = data[y * width + x];
                    }
                }
                final FaceDetecter.Face[] faceinfo = facedetecter.findFaces(ori, height, width);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        fmMask.setFaceInfo(faceinfo);


                    }
                });


                try {
                    camera.setPreviewCallback(FaceTrackActivity.this);
                } catch (Exception e) {
                    e.printStackTrace();
                }

/*
                if (!consumeFaceInfo(ori,faceinfo)){
                    try {
                        camera.setPreviewCallback(FaceTrackActivity.this);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                  //  Toast.makeText(FaceTrackActivity.this,"consumed",Toast.LENGTH_SHORT).show();

                }*/

            }
        });
    }
 
Example 20
Source File: ICamera.java    From MegviiFacepp-Android-SDK with Apache License 2.0 4 votes vote down vote up
public static ArrayList<HashMap<String, Integer>> getCameraPreviewSize(
		int cameraId) {
	ArrayList<HashMap<String, Integer>> size = new ArrayList<HashMap<String, Integer>>();
	Camera camera = null;
	try {
		camera = Camera.open(cameraId);
		if (camera == null)
			camera = Camera.open(0);

		List<Camera.Size> allSupportedSize = camera.getParameters()
				.getSupportedPreviewSizes();
		for (Camera.Size tmpSize : allSupportedSize) {
			if (tmpSize.width > tmpSize.height) {
				HashMap<String, Integer> map = new HashMap<String, Integer>();
                   map.put("width", tmpSize.width);
                   map.put("height", tmpSize.height);
                   if (tmpSize.width==640&&tmpSize.height==480){
                       size.add(map);
                   }
                   if (tmpSize.width==960&&tmpSize.height==540){
                       size.add(map);
                   }
                   if (tmpSize.width==1280&&tmpSize.height==720){
                       size.add(map);
                   }
                   if (tmpSize.width==1920&&tmpSize.height==1080){
                       size.add(map);
                   }

			}
		}

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (camera != null) {
			camera.stopPreview();
			camera.setPreviewCallback(null);
			camera.release();
			camera = null;
		}
	}

	return size;
}