Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rrogansky.dochadzkovysystem;
- import android.annotation.SuppressLint;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.graphics.SurfaceTexture;
- import android.hardware.Camera;
- import android.os.AsyncTask;
- import android.util.Base64;
- import android.util.Log;
- import android.view.Surface;
- import android.view.WindowManager;
- import com.rrogansky.dochadzkovysystem.Listeners.PhotoListener;
- import com.rrogansky.dochadzkovysystem.Methods.Helper;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.sql.Timestamp;
- import java.text.SimpleDateFormat;
- import java.util.List;
- /**
- * Created by rolan on 01.12.2015.
- */
- public class TakePhoto_new {
- private Camera mCamera;
- private Camera.Parameters parameters;
- private int mode;
- private static ProgressDialog dialog;
- private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
- public static final String BUNNY_CDN_HOSTNAME = "storage.bunnycdn.com";
- public static final String BUNNY_CDN_USERNAME = "bunnytest"; //"atenize";
- public static final String BUNNY_CDN_PASSWORD = "61469f4b-3fbe-4c8a-a240"; //"c0542a4e-e390-4178-a67a";
- @SuppressLint("StaticFieldLeak")
- public TakePhoto_new(final Context context, final String pin, final PhotoListener listener) {
- new AsyncTask<Void, Void, String>() {
- private String globalPath;
- @Override
- protected String doInBackground(Void... voids) {
- PersistStorage.getInstance().setCameraUsing(true);
- StringBuilder token = new StringBuilder();
- mode = getCameraId();
- Camera.CameraInfo info = new Camera.CameraInfo();
- for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
- Camera.getCameraInfo(i, info);
- if (info.facing == mode) {
- try {
- // Gets to here OK
- mCamera = Camera.open(i);
- SurfaceTexture dummy = new SurfaceTexture(0);
- mCamera.setPreviewTexture(dummy);
- parameters = mCamera.getParameters();
- List<Camera.Size> sizess = parameters.getSupportedPictureSizes();
- Camera.Size picture = null;
- picture = sizess.get(PersistStorage.getInstance().getCameraResolutionId());
- parameters.set("rotation", getRotation(context));
- if (picture != null) {
- parameters.setPictureSize(picture.width, picture.height);
- }
- //set camera parameters
- mCamera.setParameters(parameters);
- mCamera.startPreview();
- mCamera.takePicture(null, null, new Camera.PictureCallback() {
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
- Log.e("TAKE", "onPictureTaken");
- Log.d("employeeID", PersistStorage.getInstance().getEmployeesByPIN(pin).toString());
- listener.onTokenPhotoReceive(Base64.encodeToString(data, Base64.DEFAULT));
- if (mCamera != null) {
- mCamera.release();
- }
- PersistStorage.getInstance().setCameraUsing(false);
- FileOutputStream outStream = null;
- try {
- outStream = new FileOutputStream("/storage/emulated/0/Image.png");
- outStream.write(data);
- outStream.close();
- if (PersistStorage.getInstance().getEnableCDN()) {
- Log.e("CDN Send", "sending to CDN");
- Timestamp timestamp = new Timestamp(System.currentTimeMillis());
- File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
- File fileWithinMyDir = new File(mydir, sdf.format(timestamp) + ".jpg"); //Getting a file within the dir.
- // FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
- globalPath = fileWithinMyDir.getPath(); //"/storage/emulated/0/" + sdf.format(timestamp) + ".jpg";
- outStream = new FileOutputStream(globalPath);
- outStream.write(data);
- outStream.close();
- FTPClient con;
- try {
- con = new FTPClient();
- con.connect(BUNNY_CDN_HOSTNAME);
- if (con.login(BUNNY_CDN_USERNAME, BUNNY_CDN_PASSWORD)) {
- Log.v("FTP_CONNECTION", "Connected");
- con.enterLocalPassiveMode(); // important!
- con.setFileType(FTP.BINARY_FILE_TYPE);
- System.out.println("Uploading file: " + globalPath);
- File file = new File(globalPath);
- FileInputStream in = new FileInputStream(file);
- boolean result = con.storeFile("tablet-photos/" + file.getName(), in);
- in.close();
- if (result) Log.v("FTP_CONNECTION", "upload: succeeded");
- if (file.getCanonicalFile().delete()) Log.v("FTP_CONNECTION", "File deleted from device storage");
- con.logout();
- con.disconnect();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (FileNotFoundException e) {
- Log.e(Constants.TAG, "TAKE PHOTO 4# message: " + e.getMessage().toString());
- } catch (IOException e) {
- Log.e(Constants.TAG, "TAKE PHOTO 3# message: " + e.getMessage().toString());
- }
- }
- });
- } catch (Exception e) {
- Log.e(Constants.TAG, "TAKE PHOTO 2# message: " + e.getMessage().toString());
- token.append("Failed to connect to Camera Service");
- } finally {
- /*if (mCamera != null){
- mCamera.release();
- }*/
- // PersistStorage.getInstance().setCameraUsing(false);
- }
- }
- }
- return token.toString();
- }
- @Override
- protected void onPostExecute(String result) {
- Helper.showToast(result, context);
- }
- }.execute();
- }
- private int getCameraId() {
- if (PersistStorage.getInstance().getCameraId() == 1) {
- return Camera.CameraInfo.CAMERA_FACING_BACK;
- } else {
- return Camera.CameraInfo.CAMERA_FACING_FRONT;
- }
- }
- private int getRotation(Context context) {
- final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
- switch (rotation) {
- case Surface.ROTATION_0:
- return 0;
- case Surface.ROTATION_90:
- return 0;
- case Surface.ROTATION_180:
- return 0;
- default:
- return 180;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement