Камера не падает, проверка интернет соединения в условиях отсутствия интернета, буквенная клавиатура на вводе суммы
This commit is contained in:
@@ -47,7 +47,6 @@ flutter {
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile "com.google.android.gms:play-services-gcm:11.0.1"
|
||||
compile 'com.android.support:appcompat-v7:25.0.0'
|
||||
compile 'com.squareup.okhttp3:okhttp:3.8.1'
|
||||
compile 'com.squareup.okio:okio:1.13.0'
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
-->
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
|
||||
calls FlutterMain.startInitialization(this); in its onCreate method.
|
||||
@@ -35,9 +36,5 @@
|
||||
android:screenOrientation="portrait"
|
||||
android:theme="@style/AppTheme"/>
|
||||
|
||||
<service
|
||||
android:name="com.dinect.checker.service.RegistrationIntentService"
|
||||
android:exported="false"/>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@@ -6,11 +6,13 @@ import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.graphics.*;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.content.res.Resources ;
|
||||
import android.content.res.Resources;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.view.View;
|
||||
import android.view.Menu;
|
||||
import android.view.View.OnClickListener;
|
||||
@@ -62,50 +64,60 @@ import android.app.AlertDialog;
|
||||
import android.app.AlertDialog.Builder;
|
||||
import android.content.DialogInterface;
|
||||
import android.view.LayoutInflater;
|
||||
import com.dinect.checker.activity.Utils;
|
||||
import com.dinect.checker.R;
|
||||
|
||||
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {
|
||||
public class CameraActivity extends AppCompatActivity implements Camera.PreviewCallback {
|
||||
|
||||
private Camera mCamera;
|
||||
private Handler mAutoFocusHandler = new Handler();
|
||||
private ImageScanner mScanner = new ImageScanner();
|
||||
private HashMap<String, Integer> mContours = new HashMap<>();
|
||||
public static final String SCAN_MODES = "SCAN_MODES";
|
||||
public static final String SCAN_RESULT = "SCAN_RESULT";
|
||||
public static final String SCAN_RESULT_TYPE = "SCAN_RESULT_TYPE";
|
||||
public static final String ERROR_INFO = "ERROR_INFO";
|
||||
|
||||
private boolean mBarcodeScanned = false;
|
||||
private boolean previewing = true;
|
||||
private Handler autoFocusHandler;
|
||||
private int mOffset;
|
||||
private String mToken;
|
||||
private LogoutDialogFragment mDialog;
|
||||
private NetworkThread mNetworkThread;
|
||||
|
||||
private CameraPreview mPreview;
|
||||
private Camera mCamera;
|
||||
private ImageScanner mScanner;
|
||||
private Handler mAutoFocusHandler;
|
||||
private boolean mPreviewing = true;
|
||||
|
||||
static {
|
||||
System.loadLibrary("iconv");
|
||||
}
|
||||
|
||||
/** A safe way to get an instance of the Camera object. */
|
||||
private static Camera getCameraInstance(){
|
||||
Camera c = null;
|
||||
try {
|
||||
c = Camera.open();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.a_scanner);
|
||||
|
||||
SurfaceView preview = (SurfaceView) findViewById(R.id.cameraPreview);
|
||||
SurfaceHolder holder = preview.getHolder();
|
||||
if(!isCameraAvailable()) {
|
||||
// Cancel request if there is no rear-facing camera.
|
||||
cancelRequest();
|
||||
return;
|
||||
}
|
||||
|
||||
mOffset = (int) (56 * getResources().getDisplayMetrics().density);
|
||||
mToken = getIntent().getStringExtra(MainActivity.PREF_POS_TOKEN);
|
||||
|
||||
// Hide the window title.
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
|
||||
mAutoFocusHandler = new Handler();
|
||||
|
||||
// Create and configure the ImageScanner;
|
||||
setupScanner();
|
||||
|
||||
// Create a RelativeLayout container that will hold a SurfaceView,
|
||||
// and set it as the content of our activity.
|
||||
mPreview = new CameraPreview(this, this, autoFocusCB);
|
||||
// setContentView(mPreview);
|
||||
setContentView(R.layout.a_scanner);
|
||||
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
mOffset = (int) (56 * getResources().getDisplayMetrics().density);
|
||||
|
||||
autoFocusHandler = new Handler();
|
||||
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
|
||||
@@ -114,108 +126,123 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
actionBar.setDisplayHomeAsUpEnabled(false);
|
||||
}
|
||||
|
||||
mToken = getIntent().getStringExtra(MainActivity.PREF_POS_TOKEN);
|
||||
|
||||
if (mToken == null) {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("item", "logout");
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
FrameLayout root = (FrameLayout) findViewById(R.id.rootLayout);
|
||||
root.addView(mPreview, 0);
|
||||
}
|
||||
|
||||
mCamera = getCameraInstance();
|
||||
mCamera.setPreviewCallback(previewCallback);
|
||||
holder.addCallback(this);
|
||||
public void setupScanner() {
|
||||
mScanner = new ImageScanner();
|
||||
mScanner.setConfig(0, Config.X_DENSITY, 3);
|
||||
mScanner.setConfig(0, Config.Y_DENSITY, 3);
|
||||
|
||||
int[] symbols = getIntent().getIntArrayExtra(SCAN_MODES);
|
||||
if (symbols != null) {
|
||||
mScanner.setConfig(Symbol.NONE, Config.ENABLE, 0);
|
||||
for (int symbol : symbols) {
|
||||
mScanner.setConfig(symbol, Config.ENABLE, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mCamera.startPreview();
|
||||
previewing = true;
|
||||
|
||||
// Open the default i.e. the first rear facing camera.
|
||||
mCamera = Camera.open();
|
||||
if(mCamera == null) {
|
||||
// Cancel request if mCamera is null.
|
||||
cancelRequest();
|
||||
return;
|
||||
}
|
||||
|
||||
mPreview.setCamera(mCamera);
|
||||
mPreview.showSurfaceView();
|
||||
|
||||
mPreviewing = true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mCamera.stopPreview();
|
||||
previewing = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mCamera != null)
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
try {
|
||||
mCamera.setPreviewDisplay(holder);
|
||||
mCamera.startPreview();
|
||||
previewing = true;
|
||||
try {
|
||||
mCamera.autoFocus(autoFocusCallback);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.d("kifio", "surfaceCreated");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
setCameraDisplayOrientation(0);
|
||||
Log.d("kifio", "surfaceChanged");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
Log.d("kifio", "surfaceDestroyed");
|
||||
// Because the Camera object is a shared resource, it's very
|
||||
// important to release it when the activity is paused.
|
||||
if (mCamera != null) {
|
||||
mPreview.setCamera(null);
|
||||
mCamera.cancelAutoFocus();
|
||||
}
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.stopPreview();
|
||||
mCamera.release();
|
||||
|
||||
void setCameraDisplayOrientation(int cameraId) {
|
||||
// According to Jason Kuang on http://stackoverflow.com/questions/6519120/how-to-recover-camera-preview-from-sleep,
|
||||
// there might be surface recreation problems when the device goes to sleep. So lets just hide it and
|
||||
// recreate on resume
|
||||
mPreview.hideSurfaceView();
|
||||
|
||||
int rotation = getWindowManager().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;
|
||||
mPreviewing = false;
|
||||
mCamera = null;
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
|
||||
// получаем инфо по камере cameraId
|
||||
CameraInfo info = new CameraInfo();
|
||||
Camera.getCameraInfo(cameraId, info);
|
||||
|
||||
// задняя камера
|
||||
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
|
||||
result = ((360 - degrees) + info.orientation);
|
||||
} else
|
||||
// передняя камера
|
||||
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
|
||||
result = ((360 - degrees) - info.orientation);
|
||||
result += 360;
|
||||
}
|
||||
|
||||
public boolean isCameraAvailable() {
|
||||
PackageManager pm = getPackageManager();
|
||||
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
|
||||
}
|
||||
|
||||
public void cancelRequest() {
|
||||
Intent dataIntent = new Intent();
|
||||
dataIntent.putExtra(ERROR_INFO, "Camera unavailable");
|
||||
setResult(RESULT_CANCELED, dataIntent);
|
||||
finish();
|
||||
}
|
||||
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
if (mPreviewing) {
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
Camera.Size size = parameters.getPreviewSize();
|
||||
|
||||
Image barcode = new Image(size.width, size.height, "Y800");
|
||||
barcode.setData(data);
|
||||
barcode.setCrop((size.width / 2) - mOffset, 0, (size.width / 2) + mOffset, size.height);
|
||||
|
||||
int result = mScanner.scanImage(barcode);
|
||||
|
||||
if (result != 0) {
|
||||
mCamera.cancelAutoFocus();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.stopPreview();
|
||||
mPreviewing = false;
|
||||
SymbolSet syms = mScanner.getResults();
|
||||
for (Symbol sym : syms) {
|
||||
String symData = sym.getData();
|
||||
if (!TextUtils.isEmpty(symData) && Utils.isOnline(this)) {
|
||||
mPreviewing = false;
|
||||
requestUser(sym.getData());
|
||||
break;
|
||||
} else {
|
||||
mPreviewing = true;
|
||||
Toast.makeText(this, "Проверьте интернет соединение", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
result = result % 360;
|
||||
mCamera.setDisplayOrientation(result);
|
||||
}
|
||||
}
|
||||
|
||||
private Runnable doAutoFocus = new Runnable() {
|
||||
public void run() {
|
||||
if(mCamera != null && mPreviewing) {
|
||||
mCamera.autoFocus(autoFocusCB);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Mimic continuous auto-focusing
|
||||
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
|
||||
public void onAutoFocus(boolean success, Camera camera) {
|
||||
mAutoFocusHandler.postDelayed(doAutoFocus, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
@@ -223,11 +250,8 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
|
||||
if (item.getItemId() == R.id.logout) {
|
||||
mDialog = new LogoutDialogFragment();
|
||||
mDialog.show(getFragmentManager(), "logout");
|
||||
@@ -248,54 +272,24 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
finish();
|
||||
}
|
||||
|
||||
PreviewCallback previewCallback = new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
|
||||
Parameters parameters = camera.getParameters();
|
||||
Size size = parameters.getPreviewSize();
|
||||
|
||||
Image barcode = new Image(size.width, size.height, "Y800");
|
||||
barcode.setData(data);
|
||||
|
||||
barcode.setCrop((size.width / 2) - mOffset, 0, (size.width / 2) + mOffset, size.height);
|
||||
|
||||
int result = mScanner.scanImage(barcode);
|
||||
|
||||
if (result != 0) {
|
||||
previewing = false;
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.stopPreview();
|
||||
|
||||
SymbolSet syms = mScanner.getResults();
|
||||
|
||||
for (Symbol sym : syms) {
|
||||
mBarcodeScanned = true;
|
||||
requestUser(sym.getData());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private NetworkThread t;
|
||||
|
||||
private void requestUser(String code) {
|
||||
t = new NetworkThread(this, code, mToken);
|
||||
t.start();
|
||||
mNetworkThread = new NetworkThread(this, code, mToken);
|
||||
mNetworkThread.start();
|
||||
}
|
||||
|
||||
void handleResult(Response response, String card) {
|
||||
if (response == null) {
|
||||
previewing = true;
|
||||
mCamera.setPreviewCallback(previewCallback);
|
||||
mPreviewing = true;
|
||||
mCamera.startPreview();
|
||||
return;
|
||||
}
|
||||
if (response.code() == 200) {
|
||||
Log.d("kifio", "status: " + response.code());
|
||||
int code = response.code();
|
||||
if (code == 200) {
|
||||
try {
|
||||
String s = response.body().string();
|
||||
JSONArray users = new JSONArray(s);
|
||||
Log.d("kifio", s);
|
||||
JSONArray users = new JSONArray(s);
|
||||
if (users.length() > 0) {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("user", users.get(0).toString());
|
||||
@@ -304,14 +298,17 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(this, "Пользователь с таким id не найден", Toast.LENGTH_SHORT).show();
|
||||
previewing = true;
|
||||
mCamera.setPreviewCallback(previewCallback);
|
||||
mPreviewing = true;
|
||||
mCamera.startPreview();
|
||||
}
|
||||
} catch (IOException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if(code == 204) {
|
||||
mPreviewing = true;
|
||||
Toast.makeText(this, "Пользователь с таким id не найден", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
mPreviewing = true;
|
||||
try {
|
||||
Log.d("kifio", response.body().string());
|
||||
Toast.makeText(this, "Произошла ошибка", Toast.LENGTH_SHORT).show();
|
||||
@@ -320,28 +317,15 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
}
|
||||
}
|
||||
|
||||
t.close();
|
||||
mNetworkThread.close();
|
||||
mNetworkThread = null;
|
||||
}
|
||||
|
||||
private Runnable doAutoFocus = new Runnable() {
|
||||
public void run() {
|
||||
if (previewing) {
|
||||
mCamera.autoFocus(autoFocusCallback);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
AutoFocusCallback autoFocusCallback = new AutoFocusCallback() {
|
||||
public void onAutoFocus(boolean success, Camera camera) {
|
||||
autoFocusHandler.postDelayed(doAutoFocus, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
private static class NetworkThread extends Thread {
|
||||
|
||||
private CameraActivity mActivity;
|
||||
private String mCode = "";
|
||||
private String mUrl = "https://pos-api-int.dinect.com/20130701/?auto=";
|
||||
private String mUrl = "https://pos-api-int.dinect.com/20130701/users?auto=";
|
||||
private String mCard = "";
|
||||
private String mToken = "";
|
||||
|
||||
@@ -363,6 +347,9 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Log.d("kifio", mUrl);
|
||||
Log.d("kifio", mToken);
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.addHeader("DM-Authorization", "dmapptoken 9fec83cdca38c357e6b65dbb17514cdd36bf2a08")
|
||||
.addHeader("Authorization", "dmtoken " + mToken)
|
||||
@@ -416,7 +403,6 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
negativeButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.d("kifio", "Negative click!");
|
||||
((CameraActivity) getActivity()).dismissDialog();
|
||||
}
|
||||
});
|
||||
@@ -424,7 +410,6 @@ public class CameraActivity extends AppCompatActivity implements SurfaceHolder.C
|
||||
positiveButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.d("kifio", "Positive click!");
|
||||
((CameraActivity) getActivity()).logout();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
package com.dinect.checker.activity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.app.Activity;
|
||||
import android.hardware.Camera;
|
||||
import android.hardware.Camera.CameraInfo;
|
||||
import android.hardware.Camera.AutoFocusCallback;
|
||||
import android.hardware.Camera.PreviewCallback;
|
||||
import android.hardware.Camera.Size;
|
||||
import android.util.Log;
|
||||
import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
|
||||
private final String TAG = "CameraPreview";
|
||||
|
||||
SurfaceView mSurfaceView;
|
||||
SurfaceHolder mHolder;
|
||||
Size mPreviewSize;
|
||||
List<Size> mSupportedPreviewSizes;
|
||||
Camera mCamera;
|
||||
PreviewCallback mPreviewCallback;
|
||||
AutoFocusCallback mAutoFocusCallback;
|
||||
|
||||
CameraPreview(Context context, PreviewCallback previewCallback, AutoFocusCallback autoFocusCb) {
|
||||
super(context);
|
||||
|
||||
mPreviewCallback = previewCallback;
|
||||
mAutoFocusCallback = autoFocusCb;
|
||||
mSurfaceView = new SurfaceView(context);
|
||||
addView(mSurfaceView);
|
||||
|
||||
// Install a SurfaceHolder.Callback so we get notified when the
|
||||
// underlying surface is created and destroyed.
|
||||
mHolder = mSurfaceView.getHolder();
|
||||
mHolder.addCallback(this);
|
||||
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||
}
|
||||
|
||||
public void setCamera(Camera camera) {
|
||||
mCamera = camera;
|
||||
if (mCamera != null) {
|
||||
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
|
||||
requestLayout();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
// We purposely disregard child measurements because act as a
|
||||
// wrapper to a SurfaceView that centers the camera preview instead
|
||||
// of stretching it.
|
||||
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
|
||||
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
|
||||
setMeasuredDimension(width, height);
|
||||
|
||||
if (mSupportedPreviewSizes != null) {
|
||||
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
if (changed && getChildCount() > 0) {
|
||||
final View child = getChildAt(0);
|
||||
|
||||
final int width = r - l;
|
||||
final int height = b - t;
|
||||
|
||||
int previewWidth = width;
|
||||
int previewHeight = height;
|
||||
if (mPreviewSize != null) {
|
||||
previewWidth = mPreviewSize.width;
|
||||
previewHeight = mPreviewSize.height;
|
||||
}
|
||||
|
||||
child.layout(0, 0,width, height);
|
||||
}
|
||||
}
|
||||
|
||||
public void hideSurfaceView() {
|
||||
mSurfaceView.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
public void showSurfaceView() {
|
||||
mSurfaceView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
// The Surface has been created, acquire the camera and tell it where
|
||||
// to draw.
|
||||
try {
|
||||
if (mCamera != null) {
|
||||
mCamera.setPreviewDisplay(holder);
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
// Surface will be destroyed when we return, so stop the preview.
|
||||
if (mCamera != null) {
|
||||
mCamera.cancelAutoFocus();
|
||||
mCamera.stopPreview();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
|
||||
final double ASPECT_TOLERANCE = 0.1;
|
||||
double targetRatio = (double) w / h;
|
||||
if (sizes == null) return null;
|
||||
|
||||
Size optimalSize = null;
|
||||
double minDiff = Double.MAX_VALUE;
|
||||
|
||||
int targetHeight = h;
|
||||
|
||||
// Try to find an size match aspect ratio and size
|
||||
for (Size size : sizes) {
|
||||
double ratio = (double) size.width / size.height;
|
||||
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
|
||||
if (Math.abs(size.height - targetHeight) < minDiff) {
|
||||
optimalSize = size;
|
||||
minDiff = Math.abs(size.height - targetHeight);
|
||||
}
|
||||
}
|
||||
|
||||
// Cannot find the one match the aspect ratio, ignore the requirement
|
||||
if (optimalSize == null) {
|
||||
minDiff = Double.MAX_VALUE;
|
||||
for (Size size : sizes) {
|
||||
if (Math.abs(size.height - targetHeight) < minDiff) {
|
||||
optimalSize = size;
|
||||
minDiff = Math.abs(size.height - targetHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
return optimalSize;
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
||||
if (holder.getSurface() == null){
|
||||
// preview surface does not exist
|
||||
return;
|
||||
}
|
||||
|
||||
if (mCamera != null) {
|
||||
// Now that the size is known, set up the camera parameters and begin
|
||||
// the preview.
|
||||
setCameraDisplayOrientation(0);
|
||||
Camera.Parameters parameters = mCamera.getParameters();
|
||||
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
||||
requestLayout();
|
||||
|
||||
mCamera.setParameters(parameters);
|
||||
mCamera.setPreviewCallback(mPreviewCallback);
|
||||
mCamera.startPreview();
|
||||
mCamera.autoFocus(mAutoFocusCallback);
|
||||
}
|
||||
}
|
||||
|
||||
void setCameraDisplayOrientation(int cameraId) {
|
||||
|
||||
int rotation = ((Activity) getContext()).getWindowManager().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 = 0;
|
||||
|
||||
// получаем инфо по камере cameraId
|
||||
CameraInfo info = new CameraInfo();
|
||||
Camera.getCameraInfo(cameraId, info);
|
||||
|
||||
// задняя камера
|
||||
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
|
||||
result = ((360 - degrees) + info.orientation);
|
||||
} else
|
||||
// передняя камера
|
||||
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
|
||||
result = ((360 - degrees) - info.orientation);
|
||||
result += 360;
|
||||
}
|
||||
result = result % 360;
|
||||
mCamera.setDisplayOrientation(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +1,17 @@
|
||||
package com.dinect.checker.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import com.dinect.checker.activity.CameraActivity;
|
||||
import com.dinect.checker.service.RegistrationIntentService;
|
||||
import io.flutter.app.FlutterActivity;
|
||||
import io.flutter.plugins.GeneratedPluginRegistrant;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
import com.google.android.gms.iid.InstanceID;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.lang.System;
|
||||
@@ -43,15 +41,6 @@ public class MainActivity extends FlutterActivity {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
switch (call.method) {
|
||||
case "getInstanceID":
|
||||
InstanceID instanceID = InstanceID.getInstance(MainActivity.this);
|
||||
String id = instanceID.getId();
|
||||
if (id != null) {
|
||||
result.success(id);
|
||||
} else {
|
||||
result.error("UNAVAILABLE", "Can't get instanceID.", null);
|
||||
}
|
||||
break;
|
||||
case "saveToken":
|
||||
Map tokenArguments = call.arguments();
|
||||
mPreferences.edit().putString(PREF_POS_TOKEN, (String) tokenArguments.get("token")).apply();
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.dinect.checker.activity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.ConnectivityManager;
|
||||
|
||||
public class Utils {
|
||||
|
||||
private Utils() {}
|
||||
|
||||
public static boolean isOnline(Context context) {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo netInfo = cm.getActiveNetworkInfo();
|
||||
return netInfo != null && netInfo.isConnected();
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.dinect.checker.service;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.widget.Toast;
|
||||
import com.google.android.gms.iid.InstanceID;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RegistrationIntentService extends IntentService {
|
||||
|
||||
public RegistrationIntentService() {
|
||||
super(RegistrationIntentService.class.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(@Nullable Intent intent) {
|
||||
InstanceID instanceID = InstanceID.getInstance(this);
|
||||
String token = instanceID.getId();
|
||||
Log.d("kifio", token);
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,6 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<SurfaceView android:id="@+id/cameraPreview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
<LinearLayout android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
Reference in New Issue
Block a user