Created
November 13, 2014 21:38
-
-
Save akira-sasaki/d58fa58e6c6bd02aab0c to your computer and use it in GitHub Desktop.
Preview.java (MyCamera )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gclue.com.camera; | |
import android.content.Context; | |
import android.hardware.Camera; | |
import android.util.Log; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import java.util.List; | |
class Preview extends SurfaceView implements SurfaceHolder.Callback { | |
private static final String TAG = "MyCamera"; | |
private Camera mCamera; | |
Preview(Context context) { | |
super(context); | |
getHolder().addCallback(this); | |
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
Log.d(TAG, "onLayout l:"+l+"t:"+t+"r:"+r+"b:"+b); | |
} | |
@Override | |
public void surfaceCreated(SurfaceHolder holder) { | |
Log.d(TAG, "surfaceCreated"); | |
mCamera = Camera.open(); | |
try { | |
mCamera.setPreviewDisplay(holder); | |
} catch (Exception e){ } | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
Log.d(TAG, "surfaceChanged format:"+format+"width:"+width+"height:"+height); | |
Camera.Parameters mParams = mCamera.getParameters(); | |
List<Camera.Size> mPreviewSize = mCamera.getParameters().getSupportedPreviewSizes(); | |
Camera.Size opticalSize = getOptimalPreviewSize(mPreviewSize, width, height); | |
mParams.setPreviewSize(opticalSize.width, opticalSize.height); | |
mCamera.setParameters(mParams); | |
mCamera.startPreview(); | |
} | |
@Override | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
Log.d(TAG, "surfaceDestroyed"); | |
} | |
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) { | |
final double ASPECT_TOLERANCE = 0.05; | |
double targetRatio = (double) w / h; | |
if (sizes == null) return null; | |
Camera.Size optimalSize = null; | |
double minDiff = Double.MAX_VALUE; | |
int targetHeight = h; | |
// Try to find an size match aspect ratio and size | |
for (Camera.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 (Camera.Size size : sizes) { | |
if (Math.abs(size.height - targetHeight) < minDiff) { | |
optimalSize = size; | |
minDiff = Math.abs(size.height - targetHeight); | |
} | |
} | |
} | |
return optimalSize; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment