@ -66,6 +66,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
private int mCameraWidth ;
private int mCameraWidth ;
private int mCameraHeight ;
private int mCameraHeight ;
private boolean mCapturing ;
@Override
@Override
protected void onCreate ( Bundle savedInstanceState ) {
protected void onCreate ( Bundle savedInstanceState ) {
@ -97,17 +98,26 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
@Override
@Override
protected void onPause ( ) {
protected void onPause ( ) {
camera . stop ( ) ;
super . onPause ( ) ;
super . onPause ( ) ;
camera . stop ( ) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
camera . destroy ( ) ;
}
}
@OnClick ( R . id . capturePhoto )
@OnClick ( R . id . capturePhoto )
void capturePhoto ( ) {
void capturePhoto ( ) {
if ( mCapturing ) return ;
mCapturing = true ;
final long startTime = System . currentTimeMillis ( ) ;
final long startTime = System . currentTimeMillis ( ) ;
camera . setCameraListener ( new CameraListener ( ) {
camera . setCameraListener ( new CameraListener ( ) {
@Override
@Override
public void onPictureTaken ( byte [ ] jpeg ) {
public void onPictureTaken ( byte [ ] jpeg ) {
super . onPictureTaken ( jpeg ) ;
super . onPictureTaken ( jpeg ) ;
mCapturing = false ;
long callbackTime = System . currentTimeMillis ( ) ;
long callbackTime = System . currentTimeMillis ( ) ;
Bitmap bitmap = BitmapFactory . decodeByteArray ( jpeg , 0 , jpeg . length ) ;
Bitmap bitmap = BitmapFactory . decodeByteArray ( jpeg , 0 , jpeg . length ) ;
ResultHolder . dispose ( ) ;
ResultHolder . dispose ( ) ;
@ -126,30 +136,35 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
@OnClick ( R . id . captureVideo )
@OnClick ( R . id . captureVideo )
void captureVideo ( ) {
void captureVideo ( ) {
if ( mCapturing ) return ;
mCapturing = true ;
camera . setCameraListener ( new CameraListener ( ) {
camera . setCameraListener ( new CameraListener ( ) {
@Override
@Override
public void onVideoTaken ( File video ) {
public void onVideoTaken ( File video ) {
super . onVideoTaken ( video ) ;
super . onVideoTaken ( video ) ;
mCapturing = false ;
}
}
} ) ;
} ) ;
Toast . makeText ( this , "Recording for 3 seconds..." , Toast . LENGTH_LONG ) . show ( ) ;
camera . startRecordingVideo ( ) ;
camera . startCapturingVideo ( null ) ;
camera . postDelayed ( new Runnable ( ) {
camera . postDelayed ( new Runnable ( ) {
@Override
@Override
public void run ( ) {
public void run ( ) {
camera . stopRecord ingVideo ( ) ;
camera . stopCaptur ingVideo ( ) ;
}
}
} , 3000 ) ;
} , 3000 ) ;
}
}
@OnClick ( R . id . toggleCamera )
@OnClick ( R . id . toggleCamera )
void toggleCamera ( ) {
void toggleCamera ( ) {
if ( mCapturing ) return ;
switch ( camera . toggleFacing ( ) ) {
switch ( camera . toggleFacing ( ) ) {
case CameraKit . Constants . FACING_BACK :
case CameraKit . Constants . FACING_BACK :
Toast . makeText ( this , "Switched to back camera!" , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( this , "Switched to back camera!" , Toast . LENGTH_SHORT ) . show ( ) ;
break ;
break ;
case CameraKit . Constants . FACING_FRONT :
case CameraKit . Constants . FACING_FRONT :
Toast . makeText ( this , "Switched to front camera!" , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( this , "Switched to front camera!" , Toast . LENGTH_SHORT ) . show ( ) ;
break ;
break ;
}
}
@ -157,6 +172,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
@OnClick ( R . id . toggleFlash )
@OnClick ( R . id . toggleFlash )
void toggleFlash ( ) {
void toggleFlash ( ) {
if ( mCapturing ) return ;
switch ( camera . toggleFlash ( ) ) {
switch ( camera . toggleFlash ( ) ) {
case CameraKit . Constants . FLASH_ON :
case CameraKit . Constants . FLASH_ON :
Toast . makeText ( this , "Flash on!" , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( this , "Flash on!" , Toast . LENGTH_SHORT ) . show ( ) ;
@ -175,29 +191,28 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
RadioGroup . OnCheckedChangeListener captureModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
RadioGroup . OnCheckedChangeListener captureModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
@Override
@Override
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
camera . setMethod (
if ( mCapturing ) return ;
camera . setSessionType (
checkedId = = R . id . modeCaptureStandard ?
checkedId = = R . id . modeCaptureStandard ?
CameraKit . Constants . METHOD_STANDARD :
CameraKit . Constants . SESSION_TYPE_PICTURE :
CameraKit . Constants . METHOD_STILL
CameraKit . Constants . SESSION_TYPE_VIDEO
) ;
) ;
Toast . makeText ( MainActivity . this , "Session type set to" + ( checkedId = = R . id . modeCaptureStandard ? " picture!" : " video!" ) , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( MainActivity . this , "Picture capture set to" + ( checkedId = = R . id . modeCaptureStandard ? " quality!" : " speed!" ) , Toast . LENGTH_SHORT ) . show ( ) ;
}
}
} ;
} ;
RadioGroup . OnCheckedChangeListener cropModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
RadioGroup . OnCheckedChangeListener cropModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
@Override
@Override
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
camera . setCropOutput (
if ( mCapturing ) return ;
checkedId = = R . id . modeCropVisible
camera . setCropOutput ( checkedId = = R . id . modeCropVisible ) ;
) ;
Toast . makeText ( MainActivity . this , "Picture cropping is" + ( checkedId = = R . id . modeCropVisible ? " on!" : " off!" ) , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( MainActivity . this , "Picture cropping is" + ( checkedId = = R . id . modeCropVisible ? " on!" : " off!" ) , Toast . LENGTH_SHORT ) . show ( ) ;
}
}
} ;
} ;
@OnClick ( R . id . widthUpdate )
@OnClick ( R . id . widthUpdate )
void widthUpdateClicked ( ) {
void widthUpdateClicked ( ) {
if ( mCapturing ) return ;
if ( widthUpdate . getAlpha ( ) > = 1 ) {
if ( widthUpdate . getAlpha ( ) > = 1 ) {
updateCamera ( true , false ) ;
updateCamera ( true , false ) ;
}
}
@ -206,6 +221,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
RadioGroup . OnCheckedChangeListener widthModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
RadioGroup . OnCheckedChangeListener widthModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
@Override
@Override
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
if ( mCapturing ) return ;
widthUpdate . setEnabled ( checkedId = = R . id . widthCustom ) ;
widthUpdate . setEnabled ( checkedId = = R . id . widthCustom ) ;
widthUpdate . setAlpha ( checkedId = = R . id . widthCustom ? 1f : 0 . 3f ) ;
widthUpdate . setAlpha ( checkedId = = R . id . widthCustom ? 1f : 0 . 3f ) ;
width . clearFocus ( ) ;
width . clearFocus ( ) ;
@ -218,6 +234,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
@OnClick ( R . id . heightUpdate )
@OnClick ( R . id . heightUpdate )
void heightUpdateClicked ( ) {
void heightUpdateClicked ( ) {
if ( mCapturing ) return ;
if ( heightUpdate . getAlpha ( ) > = 1 ) {
if ( heightUpdate . getAlpha ( ) > = 1 ) {
updateCamera ( false , true ) ;
updateCamera ( false , true ) ;
}
}
@ -226,6 +243,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
RadioGroup . OnCheckedChangeListener heightModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
RadioGroup . OnCheckedChangeListener heightModeChangedListener = new RadioGroup . OnCheckedChangeListener ( ) {
@Override
@Override
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
public void onCheckedChanged ( RadioGroup group , int checkedId ) {
if ( mCapturing ) return ;
heightUpdate . setEnabled ( checkedId = = R . id . heightCustom ) ;
heightUpdate . setEnabled ( checkedId = = R . id . heightCustom ) ;
heightUpdate . setAlpha ( checkedId = = R . id . heightCustom ? 1f : 0 . 3f ) ;
heightUpdate . setAlpha ( checkedId = = R . id . heightCustom ? 1f : 0 . 3f ) ;
height . clearFocus ( ) ;
height . clearFocus ( ) ;
@ -237,6 +255,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
} ;
} ;
private void updateCamera ( boolean updateWidth , boolean updateHeight ) {
private void updateCamera ( boolean updateWidth , boolean updateHeight ) {
if ( mCapturing ) return ;
ViewGroup . LayoutParams cameraLayoutParams = camera . getLayoutParams ( ) ;
ViewGroup . LayoutParams cameraLayoutParams = camera . getLayoutParams ( ) ;
int width = cameraLayoutParams . width ;
int width = cameraLayoutParams . width ;
int height = cameraLayoutParams . height ;
int height = cameraLayoutParams . height ;
@ -290,7 +309,6 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
cameraLayoutParams . width = width ;
cameraLayoutParams . width = width ;
cameraLayoutParams . height = height ;
cameraLayoutParams . height = height ;
camera . addOnLayoutChangeListener ( this ) ;
camera . addOnLayoutChangeListener ( this ) ;
camera . setLayoutParams ( cameraLayoutParams ) ;
camera . setLayoutParams ( cameraLayoutParams ) ;
@ -301,10 +319,8 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
public void onLayoutChange ( View v , int left , int top , int right , int bottom , int oldLeft , int oldTop , int oldRight , int oldBottom ) {
public void onLayoutChange ( View v , int left , int top , int right , int bottom , int oldLeft , int oldTop , int oldRight , int oldBottom ) {
mCameraWidth = right - left ;
mCameraWidth = right - left ;
mCameraHeight = bottom - top ;
mCameraHeight = bottom - top ;
width . setText ( String . valueOf ( mCameraWidth ) ) ;
width . setText ( String . valueOf ( mCameraWidth ) ) ;
height . setText ( String . valueOf ( mCameraHeight ) ) ;
height . setText ( String . valueOf ( mCameraHeight ) ) ;
camera . removeOnLayoutChangeListener ( this ) ;
camera . removeOnLayoutChangeListener ( this ) ;
}
}