Advertisement
Guest User

Untitled

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