use of android.hardware.Camera in project barcodescanner by dm77.
the class ZBarScannerView method onPreviewFrame.
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (mResultHandler == null) {
return;
}
try {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
int width = size.width;
int height = size.height;
if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
int rotationCount = getRotationCount();
if (rotationCount == 1 || rotationCount == 3) {
int tmp = width;
width = height;
height = tmp;
}
data = getRotatedData(data, camera);
}
Rect rect = getFramingRectInPreview(width, height);
Image barcode = new Image(width, height, "Y800");
barcode.setData(data);
barcode.setCrop(rect.left, rect.top, rect.width(), rect.height());
int result = mScanner.scanImage(barcode);
if (result != 0) {
SymbolSet syms = mScanner.getResults();
final Result rawResult = new Result();
for (Symbol sym : syms) {
// In order to retreive QR codes containing null bytes we need to
// use getDataBytes() rather than getData() which uses C strings.
// Weirdly ZBar transforms all data to UTF-8, even the data returned
// by getDataBytes() so we have to decode it as UTF-8.
String symData;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
symData = new String(sym.getDataBytes(), StandardCharsets.UTF_8);
} else {
symData = sym.getData();
}
if (!TextUtils.isEmpty(symData)) {
rawResult.setContents(symData);
rawResult.setBarcodeFormat(BarcodeFormat.getFormatById(sym.getType()));
break;
}
}
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
// Stopping the preview can take a little long.
// So we want to set result handler to null to discard subsequent calls to
// onPreviewFrame.
ResultHandler tmpResultHandler = mResultHandler;
mResultHandler = null;
stopCameraPreview();
if (tmpResultHandler != null) {
tmpResultHandler.handleResult(rawResult);
}
}
});
} else {
camera.setOneShotPreviewCallback(this);
}
} catch (RuntimeException e) {
// TODO: Terrible hack. It is possible that this method is invoked after camera is released.
Log.e(TAG, e.toString(), e);
}
}
use of android.hardware.Camera in project streaming-android by red5pro.
the class PublishCameraSwapTest method openBackFacingCameraGingerbread.
protected Camera openBackFacingCameraGingerbread() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
System.out.println("Number of cameras: " + cameraCount);
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
try {
cam = Camera.open(camIdx);
camOrientation = cameraInfo.orientation;
applyInverseDeviceRotation();
break;
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
return cam;
}
use of android.hardware.Camera in project streaming-android by red5pro.
the class PublishCameraSwapTest method onPublishTouch.
private boolean onPublishTouch(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_UP && publish != null) {
R5Camera publishCam = (R5Camera) publish.getVideoSource();
Camera newCam = null;
// NOTE: Some devices will throw errors if you have a camera open when you attempt to open another
publishCam.getCamera().stopPreview();
publishCam.getCamera().release();
// NOTE: The front facing camera needs to be 180 degrees further rotated than the back facing camera
int rotate = 0;
if (currentCamMode == Camera.CameraInfo.CAMERA_FACING_FRONT) {
newCam = openBackFacingCameraGingerbread();
rotate = 0;
if (newCam != null)
currentCamMode = Camera.CameraInfo.CAMERA_FACING_BACK;
} else {
newCam = openFrontFacingCameraGingerbread();
rotate = 180;
if (newCam != null)
currentCamMode = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
if (newCam != null) {
newCam.setDisplayOrientation((camOrientation + rotate) % 360);
publishCam.setCamera(newCam);
publishCam.setOrientation(camOrientation);
newCam.startPreview();
}
}
return true;
}
use of android.hardware.Camera in project bitcoin-wallet by bitcoin-wallet.
the class CameraManager method open.
public Camera open(final TextureView textureView, final int displayOrientation) throws IOException {
final int cameraId = determineCameraId();
Camera.getCameraInfo(cameraId, cameraInfo);
log.info("opening camera id {}: {}-facing, camera orientation: {}, display orientation: {}", cameraId, cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK ? "back" : "front", cameraInfo.orientation, displayOrientation);
camera = Camera.open(cameraId);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
camera.setDisplayOrientation((720 - displayOrientation - cameraInfo.orientation) % 360);
else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
camera.setDisplayOrientation((720 - displayOrientation + cameraInfo.orientation) % 360);
else
throw new IllegalStateException("facing: " + cameraInfo.facing);
camera.setPreviewTexture(textureView.getSurfaceTexture());
final Camera.Parameters parameters = camera.getParameters();
cameraResolution = findBestPreviewSizeValue(parameters, textureView.getWidth(), textureView.getHeight());
final int width = textureView.getWidth();
final int height = textureView.getHeight();
final int rawSize = Math.min(width * 2 / 3, height * 2 / 3);
final int frameSize = Math.max(MIN_FRAME_SIZE, Math.min(MAX_FRAME_SIZE, rawSize));
final int leftOffset = (width - frameSize) / 2;
final int topOffset = (height - frameSize) / 2;
frame = new Rect(leftOffset, topOffset, leftOffset + frameSize, topOffset + frameSize);
framePreview = new RectF(frame.left * cameraResolution.width / width, frame.top * cameraResolution.height / height, frame.right * cameraResolution.width / width, frame.bottom * cameraResolution.height / height);
final String savedParameters = parameters == null ? null : parameters.flatten();
try {
setDesiredCameraParameters(camera, cameraResolution);
} catch (final RuntimeException x) {
if (savedParameters != null) {
final Camera.Parameters parameters2 = camera.getParameters();
parameters2.unflatten(savedParameters);
try {
camera.setParameters(parameters2);
setDesiredCameraParameters(camera, cameraResolution);
} catch (final RuntimeException x2) {
log.info("problem setting camera parameters", x2);
}
}
}
try {
camera.startPreview();
return camera;
} catch (final RuntimeException x) {
log.warn("something went wrong while starting camera preview", x);
camera.release();
throw x;
}
}
use of android.hardware.Camera in project BoofCV by lessthanoptimal.
the class VideoActivity method openConfigureCamera.
@Override
protected Camera openConfigureCamera(Camera.CameraInfo cameraInfo) {
Camera mCamera = selectAndOpenCamera(cameraInfo);
Camera.Parameters param = mCamera.getParameters();
// Select the preview size closest to 320x240
// Smaller images are recommended because some computer vision operations are very expensive
List<Camera.Size> sizes = param.getSupportedPreviewSizes();
Camera.Size s = sizes.get(closest(sizes, 320, 240));
param.setPreviewSize(s.width, s.height);
mCamera.setParameters(param);
return mCamera;
}
Aggregations