1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
public class CameraPreview extends Activity
{
private Preview mPreview = null ;
/** Called when the activity is first created.*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window.FEATURE_NO_TITLE) ; // Hide the window title.
AugmentedReality augrea = new AugmentedReality(this) ;
mPreview = new Preview(this, augrea) ; // Create our Preview view and set it as the content of our activity.
FrameLayout frameLayout = new FrameLayout(this) ;
frameLayout.addView(mPreview) ;
frameLayout.addView(augrea) ;
setContentView(frameLayout) ;
}
}
class AugmentedReality extends View
{
public Infos infos = null ;
public AugmentedReality(Context context)
{
super(context) ;
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas) ;
// Je dessine ce qui doit l'être en fonction de ce qui est passé dans Infos.
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder mHolder ;
private Camera mCamera ;
private AugmentedReality augrea = null ;
public Preview(Context context, AugmentedReality augrea)
{
super(context) ;
// Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed.
mHolder = getHolder() ;
mHolder.addCallback(this) ;
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) ;
this.augrea = augrea ;
try {
// Traitement préliminaire...
}
catch ( Error E )
{
System.exit(0) ;
}
catch ( Exception E )
{
E.printStackTrace() ;
System.exit(0) ;
}
Log.w("Start", "Start Camera Preview") ;
}
public void surfaceCreated(SurfaceHolder holder)
{
// The Surface has been created, acquire the camera and tell it where to draw.
mCamera = Camera.open() ;
mCamera.setPreviewCallback(
new PreviewCallback()
{
public void onPreviewFrame(byte[] data, Camera camera)
{
augrea.matchedPoints = null ;
Camera.Parameters parameters = camera.getParameters() ;
int height = parameters.getPreviewSize().height ;
int width = parameters.getPreviewSize().width ;
// Traitement du tableau data.
augrea.infos = ... ; // On passe à l'autre classe les informations à afficher.
augrea.invalidate() ; // On force le ré-affichage...
}
}
) ;
try {
mCamera.setPreviewDisplay(holder) ;
}
catch ( Exception e )
{
e.printStackTrace() ;
mCamera.release() ;
mCamera = null ;
System.exit(0) ;
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very important to release it when the activity is paused.
mCamera.stopPreview() ;
mCamera.release() ;
mCamera = null ;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h)
{
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes)
{
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null)
{
minDiff = Double.MAX_VALUE;
for (Size size : sizes)
{
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
// Now that the size is known, set up the camera parameters and begin the preview.
Camera.Parameters parameters = mCamera.getParameters() ;
List<Size> sizes = parameters.getSupportedPreviewSizes() ;
Size optimalSize = getOptimalPreviewSize(sizes, w, h) ;
parameters.setPreviewSize(optimalSize.width, optimalSize.height) ;
mCamera.setParameters(parameters) ;
mCamera.startPreview() ;
}
} |
Partager