CameraUtils.java 8.34 KB
package com.bitstrips.imoji.hardware;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Base64;
import android.util.Log;
import com.bitstrips.imoji.injection.ForApplication;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Inject;

public class CameraUtils
{
  public static final String CAMERA_PERMISSIONS = "android.permission.CAMERA";
  public static final String TAG = "CameraUtils";
  private static int a = 768;
  private static int b = 90;
  private static int c = 300000;
  private static String d = "data:image/jpeg;base64,";
  private final Context e;
  
  @Inject
  public CameraUtils(@ForApplication Context paramContext)
  {
    this.e = paramContext;
  }
  
  public static int calculateInSampleSize(BitmapFactory.Options paramOptions, int paramInt1, int paramInt2)
  {
    int k = paramOptions.outHeight;
    int m = paramOptions.outWidth;
    int j = 1;
    int i = 1;
    if ((k > paramInt2) || (m > paramInt1))
    {
      k /= 2;
      m /= 2;
      for (;;)
      {
        if (k / i < paramInt2)
        {
          j = i;
          if (m / i < paramInt1) {
            break;
          }
        }
        i *= 2;
      }
    }
    return j;
  }
  
  public static BitmapFactory.Options decodeBounds(InputStream paramInputStream)
    throws IOException
  {
    BitmapFactory.Options localOptions = new BitmapFactory.Options();
    localOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(paramInputStream, null, localOptions);
    paramInputStream.reset();
    return localOptions;
  }
  
  public static String getBase64String(Bitmap paramBitmap)
  {
    if (paramBitmap == null) {}
    for (;;)
    {
      return null;
      try
      {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        paramBitmap.compress(Bitmap.CompressFormat.JPEG, b, localByteArrayOutputStream);
        paramBitmap = Base64.encodeToString(localByteArrayOutputStream.toByteArray(), 0);
        paramBitmap = d + paramBitmap;
        int i = paramBitmap.length();
        int j = c;
        if (i <= j) {
          return paramBitmap;
        }
      }
      catch (Exception paramBitmap) {}
    }
    return null;
  }
  
  public static Bitmap getDownScaledBitmap(InputStream paramInputStream, int paramInt1, int paramInt2)
  {
    try
    {
      Object localObject = decodeBounds(paramInputStream);
      BitmapFactory.Options localOptions = new BitmapFactory.Options();
      localOptions.inSampleSize = calculateInSampleSize((BitmapFactory.Options)localObject, paramInt1, paramInt2);
      localObject = BitmapFactory.decodeStream(paramInputStream, null, localOptions);
      paramInputStream.reset();
      return (Bitmap)localObject;
    }
    catch (Exception paramInputStream) {}
    return null;
  }
  
  public static Bitmap getScaledBitmap(InputStream paramInputStream, int paramInt1, int paramInt2)
  {
    try
    {
      Object localObject = decodeBounds(paramInputStream);
      float f = Math.max(((BitmapFactory.Options)localObject).outWidth / paramInt1, ((BitmapFactory.Options)localObject).outHeight / paramInt2);
      if (f > 1.0F)
      {
        localObject = new BitmapFactory.Options();
        ((BitmapFactory.Options)localObject).inSampleSize = ((int)Math.pow(2.0D, (int)Math.ceil(Math.log(f) / Math.log(2.0D))));
      }
      for (localObject = BitmapFactory.decodeStream(paramInputStream, null, (BitmapFactory.Options)localObject);; localObject = BitmapFactory.decodeStream(paramInputStream))
      {
        paramInputStream.reset();
        return (Bitmap)localObject;
      }
      return null;
    }
    catch (Exception paramInputStream) {}
  }
  
  public final Bitmap getFixedImage(CameraDetails paramCameraDetails, byte[] paramArrayOfByte, int paramInt, boolean paramBoolean)
  {
    try
    {
      ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
      if (paramBoolean) {}
      for (paramArrayOfByte = getDownScaledBitmap(localByteArrayInputStream, paramInt, paramInt);; paramArrayOfByte = getScaledBitmap(localByteArrayInputStream, paramInt, paramInt))
      {
        localByteArrayInputStream.close();
        if (paramArrayOfByte != null) {
          break;
        }
        Log.e("CameraUtils", "Could not decode jpg data.");
        return null;
      }
      paramInt = paramCameraDetails.cameraInfo.orientation;
      paramCameraDetails = new Matrix();
      paramCameraDetails.preScale(-1.0F, 1.0F);
      paramCameraDetails.postRotate(360 - paramInt);
      paramCameraDetails = Bitmap.createBitmap(paramArrayOfByte, 0, 0, paramArrayOfByte.getWidth(), paramArrayOfByte.getHeight(), paramCameraDetails, true);
      paramArrayOfByte.recycle();
      return paramCameraDetails;
    }
    catch (IOException paramCameraDetails)
    {
      Log.e("CameraUtils", "Failed to save rotated image.", paramCameraDetails);
    }
    return null;
  }
  
  public boolean hasPermissions()
  {
    return ContextCompat.checkSelfPermission(this.e, "android.permission.CAMERA") == 0;
  }
  
  public CameraDetails openFrontFacingCamera()
  {
    try
    {
      int j = Camera.getNumberOfCameras();
      int i = 0;
      while (i < j)
      {
        Camera.CameraInfo localCameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(i, localCameraInfo);
        int k = localCameraInfo.facing;
        if (k == 1) {
          try
          {
            Camera localCamera = Camera.open(i);
            if (localCamera != null)
            {
              CameraDetails localCameraDetails = new CameraDetails(localCamera, localCameraInfo);
              localCamera.getParameters().set("orientation", "portrait");
              localCamera.setDisplayOrientation(360 - localCameraInfo.orientation);
              return localCameraDetails;
            }
          }
          catch (RuntimeException localRuntimeException)
          {
            Log.e("CameraUtils", "Camera failed to open: " + localRuntimeException.getLocalizedMessage());
          }
        }
        i += 1;
      }
      return null;
    }
    catch (Exception localException)
    {
      Log.w("CameraUtils", "Error opening camera", localException);
    }
  }
  
  public void releaseCamera(@NonNull CameraDetails paramCameraDetails)
  {
    paramCameraDetails.camera.release();
  }
  
  public final boolean requestPermissions(Activity paramActivity, int paramInt)
  {
    if (!hasPermissions())
    {
      ActivityCompat.requestPermissions(paramActivity, new String[] { "android.permission.CAMERA" }, paramInt);
      return true;
    }
    return false;
  }
  
  public void takeFixedPicture(@NonNull final CameraDetails paramCameraDetails, final int paramInt, final boolean paramBoolean, @NonNull final BitmapCallback paramBitmapCallback)
  {
    paramBitmapCallback = new Camera.PictureCallback()
    {
      public final void onPictureTaken(byte[] paramAnonymousArrayOfByte, Camera paramAnonymousCamera)
      {
        paramAnonymousArrayOfByte = CameraUtils.this.getFixedImage(paramCameraDetails, paramAnonymousArrayOfByte, paramInt, paramBoolean);
        paramBitmapCallback.onPictureTaken(paramAnonymousArrayOfByte, paramCameraDetails);
      }
    };
    paramCameraDetails.camera.takePicture(null, null, paramBitmapCallback);
  }
  
  public static abstract interface BitmapCallback
  {
    public abstract void onPictureTaken(Bitmap paramBitmap, CameraUtils.CameraDetails paramCameraDetails);
  }
  
  public static class CameraDetails
  {
    public Camera camera;
    public Camera.CameraInfo cameraInfo;
    
    public CameraDetails(Camera paramCamera, Camera.CameraInfo paramCameraInfo)
    {
      this.camera = paramCamera;
      this.cameraInfo = paramCameraInfo;
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/bitstrips/imoji/hardware/CameraUtils.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */