Skip to content
Snippets Groups Projects
Commit 8cbba736 authored by Vitaliy Vashchenko's avatar Vitaliy Vashchenko
Browse files

#617 #614 #613 Removed switch. Added auto focus ( enabled if hw supports it)....

#617 #614 #613 Removed switch. Added auto focus ( enabled if hw supports it). Changed image preview size. Code refactoring.
parent a66dd5cc
Branches
Tags
No related merge requests found
......@@ -22,6 +22,9 @@ import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.ViewParent;
......@@ -35,6 +38,10 @@ import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.nic.tablexia.android.AndroidLauncher;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.ui.QRCodeScanner;
......@@ -42,14 +49,28 @@ import cz.nic.tablexia.util.ui.QRCodeScanner;
/**
* Created by Vitaliy Vashchenko on 6.10.16.
*/
public class AndroidQRCodeScanner extends QRCodeScanner implements Camera.PreviewCallback, OnCameraReadyCallback {
public class AndroidQRCodeScanner extends QRCodeScanner implements Camera.PreviewCallback {
private static final List<String> PREFERABLE_FOCUS_MODES = new ArrayList<String>();
static {
PREFERABLE_FOCUS_MODES.add(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
PREFERABLE_FOCUS_MODES.add(Camera.Parameters.FOCUS_MODE_INFINITY);
}
private final AndroidLauncher activity;
private CameraSurface cameraSurface;
private ScreenOrientationListener screenOrientationListener;
private MultiFormatReader reader;
private boolean previewCalled = false;
private boolean cameraReady = false;
private MultiFormatReader reader;
private boolean cameraReady = false;
public AndroidQRCodeScanner(AndroidLauncher activity) {
this.activity = activity;
screenOrientationListener = new ScreenOrientationListener(activity, SensorManager.SENSOR_DELAY_NORMAL);
reader = new MultiFormatReader();
}
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
......@@ -66,40 +87,17 @@ public class AndroidQRCodeScanner extends QRCodeScanner implements Camera.Previe
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
} catch (NotFoundException e) {
Log.err(getClass(), "Cannot decode bitmap!", e);
}finally {
} finally {
reader.reset();
}
}
}
@Override
public void onCameraReady() {
cameraReady = true;
if (previewCalled) startPreviewAsync();
}
private class ScreenOrientationListener extends OrientationEventListener {
public ScreenOrientationListener(Context context, int rate) {
super(context, rate);
}
@Override
public void onOrientationChanged(int i) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
if (cameraSurface != null) {
cameraSurface.orientationChanged(rotation);
}
}
}
public AndroidQRCodeScanner(AndroidLauncher activity) {
this.activity = activity;
screenOrientationListener = new ScreenOrientationListener(activity, SensorManager.SENSOR_DELAY_NORMAL);
reader = new MultiFormatReader();
}
@Override
public boolean isCameraAccessible() {
return activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
......@@ -112,7 +110,7 @@ public class AndroidQRCodeScanner extends QRCodeScanner implements Camera.Previe
if (!cameraReady) {
previewCalled = true;
}else {
} else {
startPreviewAsync();
}
}
......@@ -153,7 +151,7 @@ public class AndroidQRCodeScanner extends QRCodeScanner implements Camera.Previe
Log.debug(getClass().getSimpleName(), "prepareCamera");
if (cameraSurface == null) {
Log.debug(getClass(), "surface is null");
cameraSurface = new CameraSurface(activity, this);
cameraSurface = new CameraSurface(activity);
}
if (cameraSurface.getParent() == null) {
Log.debug(getClass(), "parent==null - adding view to parent");
......@@ -191,4 +189,93 @@ public class AndroidQRCodeScanner extends QRCodeScanner implements Camera.Previe
cameraSurface = null;
}
}
private class ScreenOrientationListener extends OrientationEventListener {
public ScreenOrientationListener(Context context, int rate) {
super(context, rate);
}
@Override
public void onOrientationChanged(int i) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
if (cameraSurface != null) {
cameraSurface.orientationChanged(rotation);
}
}
}
class CameraSurface extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
public CameraSurface(Context context) {
super(context);
getHolder().addCallback(this);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
prepareCameraParameters();
camera.setPreviewDisplay(holder);
onCameraReady();
} catch (IOException e) {
Log.err(getClass(), "Cannot change the surface!", e);
}
}
public void orientationChanged(int i) {
int orientation;
if (camera != null) {
if (i == Surface.ROTATION_180 || i == Surface.ROTATION_270) {
orientation = 180;
} else {
orientation = 0;
}
camera.setDisplayOrientation(orientation);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.setPreviewCallback(null);
camera.stopPreview();
holder.removeCallback(this);
holder.getSurface().release();
camera.release();
camera = null;
}
public Camera getCamera() {
return camera;
}
private void prepareCameraParameters() {
Camera.Parameters parameters = camera.getParameters();
List<String> supportedFocusModes = camera.getParameters().getSupportedFocusModes();
for (String focusMode : PREFERABLE_FOCUS_MODES) {
if (supportedFocusModes.contains(focusMode)) {
parameters.setFocusMode(focusMode);
break;
}
}
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
int maxSupportedWidth = -1;
int maxSupportedHeight = -1;
for (Camera.Size size : previewSizes) {
if (size.width > maxSupportedWidth) {
maxSupportedWidth = size.width;
maxSupportedHeight = size.height;
}
}
parameters.setPictureSize(maxSupportedWidth, maxSupportedHeight);
camera.setParameters(parameters);
}
}
}
package cz.nic.tablexia.android.camera;
/*
* Copyright 2012 Johnny Lish (johnnyoneeyed@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import android.content.Context;
import android.hardware.Camera;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import cz.nic.tablexia.util.Log;
public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
private OnCameraReadyCallback cameraReadyCallback;
public CameraSurface(Context context, OnCameraReadyCallback cameraReadyCallback) {
super(context);
getHolder().addCallback(this);
this.cameraReadyCallback = cameraReadyCallback;
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
camera.setPreviewDisplay(holder);
cameraReadyCallback.onCameraReady();
} catch (IOException e) {
Log.err(getClass(), "Cannot decode bitmap!", e);
}
}
public void orientationChanged(int i){
if (camera!=null) {
switch (i){
case Surface.ROTATION_90:
camera.setDisplayOrientation(0);
break;
case Surface.ROTATION_180:
camera.setDisplayOrientation(180);
break;
case Surface.ROTATION_270:
camera.setDisplayOrientation(180);
break;
default:
camera.setDisplayOrientation(0);
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.setPreviewCallback(null);
camera.stopPreview();
holder.removeCallback(this);
holder.getSurface().release();
camera.release();
camera = null;
}
public Camera getCamera() {
return camera;
}
}
\ No newline at end of file
/*
* Copyright (C) 2016 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.nic.tablexia.android.camera;
/**
* Created by Vitaliy Vashchenko on 7.10.16.
*/
public interface OnCameraReadyCallback {
void onCameraReady();
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment