Skip to content

Commit

Permalink
feat(android): add QRScanner class and majority of its methods
Browse files Browse the repository at this point in the history
Created the QRScanner and CameraPreview classes. The majority of the methods are now functional,
excluding the scan functions.
  • Loading branch information
willhay committed Jul 27, 2016
1 parent 062001b commit 7e589ef
Show file tree
Hide file tree
Showing 3 changed files with 614 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/android/CameraPreview.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.bitpay.cordova.qrscanner;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.*;
import android.view.WindowManager;

@SuppressWarnings("deprecation")

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;


public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.

if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}

// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();

android.hardware.Camera.getCameraInfo(QRScanner.getCurrentCameraId(), info);

WindowManager windowService = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int rotation = windowService.getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
mCamera.setDisplayOrientation(result);

// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){

}
}
}
Loading

0 comments on commit 7e589ef

Please sign in to comment.