2
2
3
3
import android .app .Activity ;
4
4
import android .content .ClipData ;
5
- import android .content .Context ;
6
5
import android .content .Intent ;
7
6
import android .graphics .Bitmap ;
8
7
import android .graphics .BitmapFactory ;
49
48
import ie .yesequality .yesequality .utils .BitmapUtils ;
50
49
import ie .yesequality .yesequality .views .CameraOverlayView ;
51
50
52
- public class CameraMainActivityTest extends AppCompatActivity implements TextureView .SurfaceTextureListener ,
51
+ public class CameraMainActivityTest extends AppCompatActivity implements TextureView
52
+ .SurfaceTextureListener ,
53
53
Camera .PictureCallback {
54
54
public static final String TAG = "CameraMainActivity" ;
55
55
private static final int PICTURE_QUALITY = 100 ;
@@ -64,7 +64,7 @@ public class CameraMainActivityTest extends AppCompatActivity implements Texture
64
64
@ InjectView (R .id .camera_overlay )
65
65
protected CameraOverlayView cameraOverlayView ;
66
66
67
- TextureView previewView ;
67
+ TextureView mTextureView ;
68
68
private Camera mCamera ;
69
69
70
70
private Camera .Size optimalSize ;
@@ -84,10 +84,8 @@ public class CameraMainActivityTest extends AppCompatActivity implements Texture
84
84
};
85
85
86
86
private int mSelectedBadge = 0 ;
87
+ private float mPreviewScale ;
87
88
88
- public static String getPhotoDirectory (Context context ) {
89
- return context .getExternalFilesDir (null ).getPath ();
90
- }
91
89
92
90
/**
93
91
* 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
253
251
showCustomToast ("Tap the badge!" );
254
252
}
255
253
256
- previewView = (TextureView ) findViewById (R .id .camera_fragment );
257
- previewView .setSurfaceTextureListener (this );
254
+ mTextureView = (TextureView ) findViewById (R .id .camera_fragment );
255
+ mTextureView .setSurfaceTextureListener (this );
258
256
}
259
257
260
258
@@ -321,13 +319,17 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei
321
319
width , height );
322
320
params .setPreviewSize (optimalSize .width , optimalSize .height );
323
321
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 ;
326
326
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 ));
329
330
330
- Camera .Size pictureSize = getOptimalPreviewSize (params .getSupportedPictureSizes (), width , height );
331
+ Camera .Size pictureSize = getOptimalPreviewSize (params .getSupportedPictureSizes (), width ,
332
+ height );
331
333
params .setPictureSize (pictureSize .width , pictureSize .height );
332
334
mCamera .setParameters (params );
333
335
// start preview with new settings
@@ -433,7 +435,7 @@ protected void onResume() {
433
435
super .onResume ();
434
436
selfieButton .setEnabled (true );
435
437
436
- if (previewView .getSurfaceTexture () != null && mCamera != null ) {
438
+ if (mTextureView .getSurfaceTexture () != null && mCamera != null ) {
437
439
mCamera .startPreview ();
438
440
}
439
441
}
@@ -504,18 +506,6 @@ private void showSavingPictureErrorToast() {
504
506
Toast .makeText (this , "Error saving picture" , Toast .LENGTH_SHORT ).show ();
505
507
}
506
508
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
-
519
509
/**
520
510
* A picture has been taken.
521
511
*/
@@ -528,13 +518,27 @@ public void onPictureTaken(byte[] data, Camera camera) {
528
518
529
519
matrix .postRotate ((180 + setCameraDisplayOrientation (this , mCameraId ,
530
520
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 ) {
532
536
bitmap = Bitmap .createBitmap (
533
537
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 ,
538
542
matrix ,
539
543
false
540
544
);
@@ -555,7 +559,8 @@ public void onPictureTaken(byte[] data, Camera camera) {
555
559
556
560
bitmap = BitmapUtils .cropBitmapToSquare (bitmap );
557
561
558
- bitmap = BitmapUtils .overlayBitmap (bitmap , waterMark , ivWaterMarkPic .getX (), ivWaterMarkPic .getY (),
562
+ bitmap = BitmapUtils .overlayBitmap (bitmap , waterMark , ivWaterMarkPic .getX (),
563
+ ivWaterMarkPic .getY (),
559
564
rlSurfaceLayout .getWidth (), rlSurfaceLayout .getHeight ());
560
565
561
566
0 commit comments