1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mediaframeworktest.helpers; 18 19 import junit.framework.Assert; 20 21 import android.hardware.Camera; 22 import android.hardware.Camera.Parameters; 23 import android.hardware.Camera.PictureCallback; 24 import android.hardware.Camera.ShutterCallback; 25 import android.util.Log; 26 import android.view.SurfaceHolder; 27 28 import androidx.test.InstrumentationRegistry; 29 30 import java.io.File; 31 import java.io.FileNotFoundException; 32 import java.io.FileOutputStream; 33 import java.io.IOException; 34 35 public class CameraTestHelper { 36 37 public Camera mCamera; 38 private String TAG = "CameraTestHelper"; 39 private static final int CAMERA_ID = 0; 40 private static final long WAIT_GENERIC = 3 * 1000; // 3 seconds 41 private static final long WAIT_ZOOM_ANIMATION = 5 * 1000; // 5 seconds 42 protected static final String CAMERA_STRESS_IMAGES_DIRECTORY = "cameraStressImages"; 43 private static final String CAMERA_STRESS_IMAGES_PREFIX = "camera-stress-test"; 44 private final CameraErrorCallback mCameraErrorCallback = new CameraErrorCallback(); 45 46 private final class CameraErrorCallback implements android.hardware.Camera.ErrorCallback { onError(int error, android.hardware.Camera camera)47 public void onError(int error, android.hardware.Camera camera) { 48 Assert.fail(String.format("Camera error, code: %d", error)); 49 } 50 } 51 52 private ShutterCallback shutterCallback = new ShutterCallback() { 53 @Override 54 public void onShutter() { 55 Log.v(TAG, "Shutter"); 56 } 57 }; 58 59 private PictureCallback rawCallback = new PictureCallback() { 60 @Override 61 public void onPictureTaken(byte[] data, Camera camera) { 62 Log.v(TAG, "Raw picture taken"); 63 } 64 }; 65 66 private PictureCallback jpegCallback = new PictureCallback() { 67 @Override 68 public void onPictureTaken(byte[] data, Camera camera) { 69 FileOutputStream fos = null; 70 71 try { 72 Log.v(TAG, "JPEG picture taken"); 73 fos = new FileOutputStream(String.format("%s/%s/%s-%d.jpg", 74 InstrumentationRegistry.getInstrumentation().getTargetContext() 75 .getExternalFilesDir(null).getPath(), CAMERA_STRESS_IMAGES_DIRECTORY, 76 CAMERA_STRESS_IMAGES_PREFIX, System.currentTimeMillis())); 77 fos.write(data); 78 } catch (FileNotFoundException e) { 79 Log.e(TAG, "File not found: " + e.toString()); 80 } catch (IOException e) { 81 Log.e(TAG, "Error accessing file: " + e.toString()); 82 } finally { 83 try { 84 if (fos != null) { 85 fos.close(); 86 } 87 } catch (IOException e) { 88 Log.e(TAG, "Error closing file: " + e.toString()); 89 } 90 } 91 } 92 }; 93 94 /** 95 * Helper method for prepping test 96 */ setupCameraTest()97 public void setupCameraTest() { 98 // Create the test images directory if it doesn't exist 99 File stressImagesDirectory = new File(String.format("%s/%s", 100 InstrumentationRegistry.getInstrumentation().getTargetContext() 101 .getExternalFilesDir(null).getPath(), CAMERA_STRESS_IMAGES_DIRECTORY)); 102 if (!stressImagesDirectory.exists()) { 103 stressImagesDirectory.mkdir(); 104 } 105 106 mCamera = Camera.open(CAMERA_ID); 107 } 108 109 /** 110 * Helper method for getting the available parameters of the default camera 111 */ getCameraParameters()112 public Parameters getCameraParameters() { 113 mCamera = Camera.open(CAMERA_ID); 114 Parameters params = mCamera.getParameters(); 115 mCamera.release(); 116 return params; 117 } 118 119 /** 120 * Helper method for taking a photo 121 */ capturePhoto()122 public void capturePhoto() throws Exception { 123 mCamera.takePicture(shutterCallback, rawCallback, jpegCallback); 124 Thread.sleep(WAIT_GENERIC); 125 mCamera.stopPreview(); 126 mCamera.release(); 127 } 128 129 /** 130 * Helper method for cleaning up pics taken during tests 131 */ cleanupTestImages()132 public void cleanupTestImages() { 133 try { 134 File stressImagesDirectory = new File(String.format("%s/%s", 135 InstrumentationRegistry.getInstrumentation().getTargetContext() 136 .getExternalFilesDir(null).getPath(), CAMERA_STRESS_IMAGES_DIRECTORY)); 137 File[] stressImages = stressImagesDirectory.listFiles(); 138 for (File f : stressImages) { 139 f.delete(); 140 } 141 } catch (SecurityException e) { 142 Log.e(TAG, "Security manager access violation: " + e.toString()); 143 } 144 } 145 146 /** 147 * Helper method for setting the camera parameters 148 */ setParameters(Parameters params)149 public void setParameters(Parameters params) { 150 try { 151 mCamera.setParameters(params); 152 } catch (Exception e) { 153 Log.e(TAG, "Error setting camera parameters"); 154 } 155 } 156 157 /** 158 * Helper method for starting up the camera preview 159 */ startCameraPreview(SurfaceHolder surfaceHolder)160 public void startCameraPreview(SurfaceHolder surfaceHolder) throws Exception { 161 mCamera.setErrorCallback(mCameraErrorCallback); 162 mCamera.setPreviewDisplay(surfaceHolder); 163 mCamera.startPreview(); 164 Thread.sleep(WAIT_GENERIC); 165 } 166 } 167 168