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
|
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.
mPreview = new Preview(this) ; // Create our Preview view and set it as the content of our activity.
setContentView(mPreview) ;
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
SurfaceHolder mHolder;
Camera mCamera;
public Preview(Context context)
{
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);
try {
// ... Initialisations ...
}
catch ( Error E )
{
System.exit(0) ;
}
catch ( Exception E )
{
System.exit(0) ;
}
Log.i("Start", "Start Camera Preview") ;
}
private int nb = 0 ;
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)
{ // On utilise cette méthode pour récupérer l'image de la caméra et ainsi travailler dessus.
Camera.Parameters parameters = camera.getParameters() ;
int height = parameters.getPreviewSize().height ;
int width = parameters.getPreviewSize().width ;
//... Traitement de ma vidéo ...
invalidate() ;
}
}
) ;
try {
mCamera.setPreviewDisplay(holder);
}
catch (Exception exception)
{
mCamera.release();
mCamera = null;
}
}
private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); // Lisser les formes
protected void onDraw(Canvas canvas)
{ // Pour l'instant j'essaie juste d'afficher un cercle sur ma vidéo.
super.onDraw(canvas) ;
canvas.drawColor(Color.WHITE) ;
int centerX = getMeasuredWidth() / 2 ;
int centerY = getMeasuredHeight() / 2 ;
// On détermine le diamètre du cercle (arrière plan de la boussole)
int radius = Math.min(centerX, centerY);
// On dessine un cercle avec le " pinceau " circlePaint
canvas.drawCircle(centerX, centerY, radius, circlePaint) ;
}
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