22
33import android .app .Activity ;
44import android .content .ClipData ;
5- import android .content .Context ;
65import android .content .Intent ;
76import android .graphics .Bitmap ;
87import android .graphics .BitmapFactory ;
4948import ie .yesequality .yesequality .utils .BitmapUtils ;
5049import ie .yesequality .yesequality .views .CameraOverlayView ;
5150
52- public class CameraMainActivityTest extends AppCompatActivity implements TextureView .SurfaceTextureListener ,
51+ public class CameraMainActivityTest extends AppCompatActivity implements TextureView
52+ .SurfaceTextureListener ,
5353 Camera .PictureCallback {
5454 public static final String TAG = "CameraMainActivity" ;
5555 private static final int PICTURE_QUALITY = 100 ;
@@ -64,7 +64,7 @@ public class CameraMainActivityTest extends AppCompatActivity implements Texture
6464 @ InjectView (R .id .camera_overlay )
6565 protected CameraOverlayView cameraOverlayView ;
6666
67- TextureView previewView ;
67+ TextureView mTextureView ;
6868 private Camera mCamera ;
6969
7070 private Camera .Size optimalSize ;
@@ -84,10 +84,8 @@ public class CameraMainActivityTest extends AppCompatActivity implements Texture
8484 };
8585
8686 private int mSelectedBadge = 0 ;
87+ private float mPreviewScale ;
8788
88- public static String getPhotoDirectory (Context context ) {
89- return context .getExternalFilesDir (null ).getPath ();
90- }
9189
9290 /**
9391 * Determine the current display orientation and rotate the mCamera preview
@@ -253,8 +251,8 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
253251 showCustomToast ("Tap the badge!" );
254252 }
255253
256- previewView = (TextureView ) findViewById (R .id .camera_fragment );
257- previewView .setSurfaceTextureListener (this );
254+ mTextureView = (TextureView ) findViewById (R .id .camera_fragment );
255+ mTextureView .setSurfaceTextureListener (this );
258256 }
259257
260258
@@ -321,13 +319,17 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei
321319 width , height );
322320 params .setPreviewSize (optimalSize .width , optimalSize .height );
323321
324- int smallSide = optimalSize .height < optimalSize .width ? optimalSize .height : optimalSize .width ;
325- int largeSide = optimalSize .height > optimalSize .width ? optimalSize .height : optimalSize .width ;
322+ int smallSide = optimalSize .height < optimalSize .width ? optimalSize .height : optimalSize
323+ .width ;
324+ int largeSide = optimalSize .height > optimalSize .width ? optimalSize .height : optimalSize
325+ .width ;
326326
327- float scale = (float ) rlSurfaceLayout .getWidth () / smallSide ;
328- previewView .setLayoutParams (new FrameLayout .LayoutParams (rlSurfaceLayout .getWidth (), (int ) (scale * largeSide ), Gravity .CENTER ));
327+ mPreviewScale = (float ) rlSurfaceLayout .getWidth () / smallSide ;
328+ mTextureView .setLayoutParams (new FrameLayout .LayoutParams (rlSurfaceLayout .getWidth (),
329+ (int ) (mPreviewScale * largeSide ), Gravity .CENTER ));
329330
330- Camera .Size pictureSize = getOptimalPreviewSize (params .getSupportedPictureSizes (), width , height );
331+ Camera .Size pictureSize = getOptimalPreviewSize (params .getSupportedPictureSizes (), width ,
332+ height );
331333 params .setPictureSize (pictureSize .width , pictureSize .height );
332334 mCamera .setParameters (params );
333335 // start preview with new settings
@@ -433,7 +435,7 @@ protected void onResume() {
433435 super .onResume ();
434436 selfieButton .setEnabled (true );
435437
436- if (previewView .getSurfaceTexture () != null && mCamera != null ) {
438+ if (mTextureView .getSurfaceTexture () != null && mCamera != null ) {
437439 mCamera .startPreview ();
438440 }
439441 }
@@ -504,18 +506,6 @@ private void showSavingPictureErrorToast() {
504506 Toast .makeText (this , "Error saving picture" , Toast .LENGTH_SHORT ).show ();
505507 }
506508
507- /**
508- * Take a picture and notify the listener once the picture is taken.
509- */
510- public void takePicture () {
511- if (mCamera != null ) {
512- mCamera .takePicture (null , null , this );
513- } else {
514- Toast .makeText (this , R .string .error_taking_picture , Toast .LENGTH_LONG ).show ();
515- }
516- }
517-
518-
519509 /**
520510 * A picture has been taken.
521511 */
@@ -528,13 +518,27 @@ public void onPictureTaken(byte[] data, Camera camera) {
528518
529519 matrix .postRotate ((180 + setCameraDisplayOrientation (this , mCameraId ,
530520 mCamera )) % 360 );
531- if (bitmap .getWidth () >= optimalSize .width && bitmap .getHeight () >= optimalSize .height ) {
521+ int largeSide = bitmap .getWidth () > bitmap .getHeight () ? bitmap .getWidth () : bitmap
522+ .getHeight ();
523+ int smallSide = bitmap .getWidth () < bitmap .getHeight () ? bitmap .getWidth () : bitmap
524+ .getHeight ();
525+
526+ int largePreviewSide = optimalSize .width > optimalSize .height ? optimalSize .width :
527+ optimalSize .height ;
528+ int smallPreviewSide = optimalSize .width < optimalSize .height ? optimalSize .width :
529+ optimalSize .height ;
530+
531+ Log .d (this .getClass ().getSimpleName (), "Scale: " + mPreviewScale + "\n - large: " +
532+ largeSide + ":" + largePreviewSide + "\n - small: " + smallSide + ":" +
533+ smallPreviewSide );
534+
535+ if (mPreviewScale <= 1 && largeSide >= largePreviewSide && smallSide >= smallPreviewSide ) {
532536 bitmap = Bitmap .createBitmap (
533537 bitmap ,
534- Math .abs (optimalSize . width - bitmap . getWidth () ) / 2 ,
535- Math .abs (optimalSize . height - bitmap . getHeight () ) / 2 ,
536- optimalSize . width ,
537- optimalSize . height ,
538+ Math .abs (largeSide - largePreviewSide ) / 2 ,
539+ Math .abs (smallSide - smallPreviewSide ) / 2 ,
540+ largePreviewSide ,
541+ smallPreviewSide ,
538542 matrix ,
539543 false
540544 );
@@ -555,7 +559,8 @@ public void onPictureTaken(byte[] data, Camera camera) {
555559
556560 bitmap = BitmapUtils .cropBitmapToSquare (bitmap );
557561
558- bitmap = BitmapUtils .overlayBitmap (bitmap , waterMark , ivWaterMarkPic .getX (), ivWaterMarkPic .getY (),
562+ bitmap = BitmapUtils .overlayBitmap (bitmap , waterMark , ivWaterMarkPic .getX (),
563+ ivWaterMarkPic .getY (),
559564 rlSurfaceLayout .getWidth (), rlSurfaceLayout .getHeight ());
560565
561566
0 commit comments