Advertisement
Guest User

Untitled

a guest
Jan 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.70 KB | None | 0 0
  1. package com.rrogansky.dochadzkovysystem;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.graphics.SurfaceTexture;
  6. import android.hardware.Camera;
  7. import android.os.AsyncTask;
  8. import android.util.Base64;
  9. import android.util.Log;
  10. import android.view.Surface;
  11. import android.view.WindowManager;
  12.  
  13. import com.rrogansky.dochadzkovysystem.Listeners.PhotoListener;
  14. import com.rrogansky.dochadzkovysystem.Methods.Helper;
  15.  
  16. import org.apache.commons.net.ftp.FTP;
  17. import org.apache.commons.net.ftp.FTPClient;
  18.  
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.sql.Timestamp;
  25. import java.text.SimpleDateFormat;
  26. import java.util.List;
  27.  
  28. /**
  29.  * Created by rolan on 01.12.2015.
  30.  */
  31. public class TakePhoto_new {
  32.  
  33.     private Camera mCamera;
  34.     private Camera.Parameters parameters;
  35.     private int mode;
  36.     private static ProgressDialog dialog;
  37.     private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  38.  
  39.     public static final String BUNNY_CDN_HOSTNAME = "storage.bunnycdn.com";
  40.     public static final String BUNNY_CDN_USERNAME = "bunnytest"; //"atenize";
  41.     public static final String BUNNY_CDN_PASSWORD = "61469f4b-3fbe-4c8a-a240"; //"c0542a4e-e390-4178-a67a";
  42.  
  43.     public TakePhoto_new(final Context context, final String pin, final PhotoListener listener) {
  44.         new AsyncTask<Void, Void, String>() {
  45.  
  46.             private String globalPath;
  47.  
  48.             @Override
  49.             protected String doInBackground(Void... voids) {
  50.                 PersistStorage.getInstance().setCameraUsing(true);
  51.  
  52.                 StringBuilder token = new StringBuilder();
  53.                 mode = getCameraId();
  54.  
  55.                 Camera.CameraInfo info = new Camera.CameraInfo();
  56.                 for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
  57.                     Camera.getCameraInfo(i, info);
  58.                     if (info.facing == mode) {
  59.                         try {
  60.                             // Gets to here OK
  61.                             mCamera = Camera.open(i);
  62.                             SurfaceTexture dummy = new SurfaceTexture(0);
  63.  
  64.                             mCamera.setPreviewTexture(dummy);
  65.                             parameters = mCamera.getParameters();
  66.  
  67.                             List<Camera.Size> sizess = parameters.getSupportedPictureSizes();
  68.                             Camera.Size picture = null;
  69.                             picture = sizess.get(PersistStorage.getInstance().getCameraResolutionId());
  70.  
  71.                             parameters.set("rotation", getRotation(context));
  72.  
  73.                             if (picture != null) {
  74.                                 parameters.setPictureSize(picture.width, picture.height);
  75.                             }
  76.  
  77.                             //set camera parameters
  78.                             mCamera.setParameters(parameters);
  79.                             mCamera.startPreview();
  80.                             mCamera.takePicture(null, null, new Camera.PictureCallback() {
  81.                                 @Override
  82.                                 public void onPictureTaken(byte[] data, Camera camera) {
  83.                                     Log.e("TAKE", "onPictureTaken");
  84.                                     Log.d("employeeID", PersistStorage.getInstance().getEmployeesByPIN(pin).toString());
  85.                                     listener.onTokenPhotoReceive(Base64.encodeToString(data, Base64.DEFAULT));
  86.  
  87.                                     if (mCamera != null) {
  88.                                         mCamera.release();
  89.                                     }
  90.                                     PersistStorage.getInstance().setCameraUsing(false);
  91.  
  92.                                     FileOutputStream outStream = null;
  93.                                     try {
  94.                                         outStream = new FileOutputStream("/storage/emulated/0/Image.png");
  95.                                         outStream.write(data);
  96.                                         outStream.close();
  97.                                         if (PersistStorage.getInstance().getEnableCDN()) {
  98.                                             Log.e("CDN Send", "sending to CDN");
  99. //                                            String time  = " ";
  100. //                                            Calendar c = Calendar.getInstance();
  101. //                                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd+kk&mm&ss");
  102. //                                            time =  df.format(c.getTime()).replace("&", "%3A");
  103.                                             Timestamp timestamp = new Timestamp(System.currentTimeMillis());
  104.                                             globalPath = "/storage/emulated/0/" + sdf.format(timestamp) + ".jpg";
  105.                                             outStream = new FileOutputStream(globalPath);
  106.                                             outStream.write(data);
  107.                                             outStream.close();
  108.                                         }
  109.  
  110.  
  111.                                     } catch (FileNotFoundException e) {
  112.                                         Log.e(Constants.TAG, "TAKE PHOTO 4# message: " + e.getMessage().toString());
  113.                                     } catch (IOException e) {
  114.                                         Log.e(Constants.TAG, "TAKE PHOTO 3# message: " + e.getMessage().toString());
  115.                                     }
  116.  
  117.  
  118.                                 }
  119.                             });
  120.  
  121.                         } catch (Exception e) {
  122.                             Log.e(Constants.TAG, "TAKE PHOTO 2# message: " + e.getMessage().toString());
  123.                             token.append("Failed to connect to Camera Service");
  124.                         } finally {
  125.                             /*if (mCamera != null){
  126.                                 mCamera.release();
  127.                             }*/
  128.                             // PersistStorage.getInstance().setCameraUsing(false);
  129.                         }
  130.                     }
  131.                 }
  132.                 return token.toString();
  133.             }
  134.  
  135.             @Override
  136.             protected void onPostExecute(String result) {
  137.                 Helper.showToast(result, context);
  138.                 new AsyncOperation().execute(globalPath);
  139.             }
  140.         }.execute();
  141.     }
  142.  
  143.     private int getCameraId() {
  144.         if (PersistStorage.getInstance().getCameraId() == 1) {
  145.             return Camera.CameraInfo.CAMERA_FACING_BACK;
  146.         } else {
  147.             return Camera.CameraInfo.CAMERA_FACING_FRONT;
  148.         }
  149.     }
  150.  
  151.     private int getRotation(Context context) {
  152.         final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
  153.         switch (rotation) {
  154.             case Surface.ROTATION_0:
  155.                 return 0;
  156.             case Surface.ROTATION_90:
  157.                 return 0;
  158.             case Surface.ROTATION_180:
  159.                 return 0;
  160.             default:
  161.                 return 180;
  162.         }
  163.     }
  164.  
  165.     private static class AsyncOperation extends AsyncTask<String, Void, String> {
  166.  
  167.         private void uploadImages(String imagepath) {
  168.             FTPClient con;
  169.  
  170.             try {
  171.                 con = new FTPClient();
  172.                 con.connect(BUNNY_CDN_HOSTNAME);
  173.  
  174.                 if (con.login(BUNNY_CDN_USERNAME, BUNNY_CDN_PASSWORD)) {
  175.                     Log.v("FTP_CONNECTION", "Connected");
  176.                     con.enterLocalPassiveMode(); // important!
  177.                     con.setFileType(FTP.BINARY_FILE_TYPE);
  178.                     System.out.println("Uploading file: " + imagepath);
  179.                     File file = new File(imagepath);
  180.                     FileInputStream in = new FileInputStream(file);
  181.                     boolean result = con.storeFile("tablet-photos/" + file.getName(), in);
  182.                     in.close();
  183.                     if (result) Log.v("FTP_CONNECTION", "upload: succeeded");
  184.  
  185.                     con.logout();
  186.                     con.disconnect();
  187.                 }
  188.             } catch (Exception e) {
  189.                 e.printStackTrace();
  190.             }
  191.         }
  192.  
  193.         @Override
  194.         protected String doInBackground(String... strings) {
  195.             String imagepath = strings[0];
  196.             uploadImages(imagepath);
  197.             return "Executed";
  198.         }
  199.  
  200.         @Override
  201.         protected void onPostExecute(String result) {
  202.             if (dialog != null) {
  203.                 dialog.dismiss();
  204.             }
  205. //            System.out.println("EXECUTED");
  206.         }
  207.  
  208.  
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement