DESIGN CHALLENGE

Gourish Menon
3 min readApr 11, 2021

The code in GingerBreadCameraInterface is partcularly for back camera and could be replaced.

We will be replacing the following code with

try {

Class AndroidCamera;

AndroidCamera = Class.forName(“android.hardware.Camera”);

int numCameras = ((Integer) invokeStaticMethod(getMethod(AndroidCamera, “getNumberOfCameras”))).intValue();

if (numCameras == 0) {

Log.w(TAG, “No cameras!”);

return null;

}

int index = 0;

Class CameraInfo = Class.forName(“android.hardware.Camera$CameraInfo”);

Field facingField = CameraInfo.getField(“facing”);

while (index < numCameras) {

Object cameraInfo = CameraInfo.getConstructor().newInstance();

AndroidCamera.getMethod(“getCameraInfo”, int.class, CameraInfo).invoke(null, index, cameraInfo);

if (facingField.getInt(cameraInfo) == CameraInfo.getDeclaredField(“CAMERA_FACING_BACK”).getInt(CameraInfo))

break;

index++;

}

if (index < numCameras) {

Log.i(TAG, “Opening camera #” + index);

} else {

Log.i(TAG, “No camera facing back; returning camera #0”);

index = 0;

}

By using this Particular code —

The following code checks that the particular Camera is Front or Back and then proceeds if it is front.

private int findFrontFacingCamera()

{

int cameraId = -1;

// Search for the front facing camera

int numberOfCameras = Camera.getNumberOfCameras();

for (int i = 0; i < numberOfCameras; i++) { Camera.CameraInfo info = new Camera.CameraInfo();

Camera.getCameraInfo(i, info);

if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)

{

cameraId = i;

cameraFront= true;

break;

}

We will be using this Particular code if we does not want the interruption by the user after every Shot.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

int index = getFrontCameraId();
if (index == -1){
Toast.makeText(getApplicationContext(), "No front camera", Toast.LENGTH_LONG).show();
}
else
{
iv_image = (ImageView) findViewById(R.id.imageView);
sv = (SurfaceView) findViewById(R.id.surfaceView);
sHolder = sv.getHolder();
sHolder.addCallback(this);
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.startPreview();

Camera.PictureCallback mCall = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
Uri uriTarget = getContentResolver().insert//(Media.EXTERNAL_CONTENT_URI, image);
(Media.EXTERNAL_CONTENT_URI, new ContentValues());

OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();

Toast.makeText(TakePictureActivity.this,
"Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
//mCamera.startPreview();

bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
iv_image.setImageBitmap(bmp);
}
};

mCamera.takePicture(null, null, mCall);
}

int getFrontCameraId() {
CameraInfo ci = new CameraInfo();
for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, ci);
if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
}
return -1; // No front-facing camera found
}

@Override
public void surfaceCreated(SurfaceHolder holder)
{
int index = getFrontCameraId();
if (index == -1){
Toast.makeText(getApplicationContext(), "No front camera", Toast.LENGTH_LONG).show();
}
else
{
mCamera = Camera.open(index);
Toast.makeText(getApplicationContext(), "With front camera", Toast.LENGTH_LONG).show();
}
mCamera = Camera.open(index);
try {
mCamera.setPreviewDisplay(holder);

} catch (IOException exception) {
mCamera.release();
mCamera = null;
}

}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}

It will automatically save the Picture in the gallery and we can Preview all the pictures from the gallery and the user can choose from the Pictures that he would like to choose from. The amount of photos will depend on the User and will go upto the User wants to stop taking photos and want preview it.

takepicture = (Button) findViewById(R.id.button); // creating button from main.xml
takepicture.setOnClickListener(new OnClickListener(){ // creating useful button
public void onClick(View view){
mCamera.takePicture(mShutterCallback,mPictureCallback,mjpeg); // when clicked take picture
}
});

We could add some filters and different modes like short video,GIF,etc.We could also add an Photo Collage which could add multiple photos all together.GIF CODEpublic static void main(String[] args) throws MalformedURLException {
URL url = new URL("<url_to_animated_gif>");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);

JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

We should create an New java file inside the open or could assign a function inside CameraConfigurationManager and the function should be called whenever user wants to create GIF,Short Videos.

--

--