Camera preview displays with correct aspect ratio.
This commit is contained in:
@@ -10,4 +10,11 @@
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context=".CamActivity" >
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/previewLayout"
|
||||
android:layout_width="0dip"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="0.47" >
|
||||
</FrameLayout>
|
||||
|
||||
</LinearLayout>
|
@@ -13,6 +13,7 @@ import android.support.v4.app.NavUtils;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class CamActivity extends Activity{
|
||||
@@ -33,9 +34,7 @@ public class CamActivity extends Activity{
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
|
||||
cPreview = new CameraPreview(this, hwCamera);
|
||||
setContentView(cPreview);
|
||||
setContentView(R.layout.activity_cam);
|
||||
|
||||
Intent intent = getIntent();
|
||||
serverIp = intent.getStringExtra("address");
|
||||
@@ -82,10 +81,12 @@ public class CamActivity extends Activity{
|
||||
|
||||
// TODO: pause the imThread and botThread objects.
|
||||
|
||||
if(cPreview != null){
|
||||
cPreview.removePreviewCallback();
|
||||
cPreview.setCamera(null);
|
||||
releaseCamera();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(){
|
||||
@@ -99,7 +100,11 @@ public class CamActivity extends Activity{
|
||||
******************/
|
||||
public void startCameraPreview(){
|
||||
if(hwCamera != null){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".startCameraPreview() :: Setting camera.");
|
||||
cPreview = new CameraPreview(this, hwCamera);
|
||||
cPreview.setCamera(hwCamera);
|
||||
((FrameLayout)findViewById(R.id.previewLayout)).addView(cPreview);
|
||||
Logger.log_d(TAG, CLASS_NAME + ".startCameraPreview() :: Camera and content view set.");
|
||||
}else{
|
||||
Logger.log_wtf(TAG, CLASS_NAME + ".startCameraPreview() :: CAMERA IS NULL!");
|
||||
System.exit(1);
|
||||
@@ -119,6 +124,7 @@ public class CamActivity extends Activity{
|
||||
@Override
|
||||
protected Camera doInBackground(Void... params) {
|
||||
Camera cam = null;
|
||||
Logger.log_d(TAG, CLASS_NAME + ".doInBackground() :: Opening the camera.");
|
||||
try{
|
||||
cam = Camera.open(0);
|
||||
}catch(Exception e){
|
||||
|
@@ -13,20 +13,15 @@ import android.os.Build;
|
||||
import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
/** A basic Camera preview class */
|
||||
@SuppressLint("ViewConstructor")
|
||||
public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback, Camera.PreviewCallback {
|
||||
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
|
||||
private final String TAG = "SURFVIEW";
|
||||
private final String CLASS_NAME = CameraPreview.class.getSimpleName();
|
||||
|
||||
private Size previewSize;
|
||||
private List<Size> supportedPreviewSizes;
|
||||
private CameraImageMonitor imgMonitor;
|
||||
private Activity parentActivity;
|
||||
private SurfaceView surfaceView;
|
||||
private SurfaceHolder holder;
|
||||
private Camera camera;
|
||||
|
||||
@@ -35,8 +30,8 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
|
||||
super(context);
|
||||
parentActivity = (Activity)context;
|
||||
|
||||
surfaceView = new SurfaceView(context);
|
||||
holder = surfaceView.getHolder();
|
||||
// surfaceView = new SurfaceView(context);
|
||||
holder = getHolder();
|
||||
holder.addCallback(this);
|
||||
|
||||
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB)
|
||||
@@ -46,19 +41,21 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
|
||||
public void setCamera(Camera camera){
|
||||
this.camera = camera;
|
||||
if(this.camera != null){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".setCamera() :: Setting camera.");
|
||||
imgMonitor = CameraImageMonitor.getInstance();
|
||||
supportedPreviewSizes = this.camera.getParameters().getSupportedPreviewSizes();
|
||||
requestLayout();
|
||||
Logger.log_d(TAG, CLASS_NAME + ".setCamera() :: Camera set.");
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder){
|
||||
// The Surface has been created, now tell the camera where to draw the preview.
|
||||
Logger.log_d(TAG, CLASS_NAME + ".surfaceCreated() :: Creating surface view.");
|
||||
try {
|
||||
if(camera != null)
|
||||
camera.setPreviewDisplay(holder);
|
||||
} catch (IOException e) {
|
||||
Logger.log_e(TAG, CLASS_NAME + ".surfaceCreated() :: Error setting camera preview: " + e.getMessage());
|
||||
Logger.log_e(TAG, CLASS_NAME + ".surfaceCreated() :: Error creating camera: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,10 +81,9 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
|
||||
requestLayout();
|
||||
|
||||
camParams = camera.getParameters();
|
||||
/*Size optimal = getOptimalPreviewSize(camParams.getSupportedPreviewSizes(), w, h);
|
||||
if(ProjectConstants.DEBUG)
|
||||
Log.d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")");*/
|
||||
camParams.setPreviewSize(previewSize.width, previewSize.height);
|
||||
Size optimal = getOptimalPreviewSize(camParams.getSupportedPreviewSizes(), w, h);
|
||||
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")");
|
||||
camParams.setPreviewSize(optimal.width, optimal.height);
|
||||
camera.setParameters(camParams);
|
||||
|
||||
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
||||
@@ -134,6 +130,8 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
|
||||
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
|
||||
final double ASPECT_TOLERANCE = 0.1;
|
||||
double targetRatio = (double) w / h;
|
||||
|
||||
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method started.");
|
||||
if (sizes == null) return null;
|
||||
|
||||
Size optimalSize = null;
|
||||
@@ -161,45 +159,9 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
|
||||
}
|
||||
}
|
||||
}
|
||||
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method ended.");
|
||||
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Optimal size is: (" + Integer.toString(optimalSize.width) +
|
||||
", " + Integer.toString(optimalSize.height) + ")");
|
||||
return optimalSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
if (changed && getChildCount() > 0) {
|
||||
final View child = getChildAt(0);
|
||||
|
||||
final int width = r - l;
|
||||
final int height = b - t;
|
||||
|
||||
int previewWidth = width;
|
||||
int previewHeight = height;
|
||||
if (previewSize != null) {
|
||||
previewWidth = previewSize.width;
|
||||
previewHeight = previewSize.height;
|
||||
}
|
||||
|
||||
// Center the child SurfaceView within the parent.
|
||||
if (width * previewHeight > height * previewWidth) {
|
||||
final int scaledChildWidth = previewWidth * height / previewHeight;
|
||||
child.layout((width - scaledChildWidth) / 2, 0,
|
||||
(width + scaledChildWidth) / 2, height);
|
||||
} else {
|
||||
final int scaledChildHeight = previewHeight * width / previewWidth;
|
||||
child.layout(0, (height - scaledChildHeight) / 2,
|
||||
width, (height + scaledChildHeight) / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
|
||||
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
|
||||
setMeasuredDimension(width, height);
|
||||
|
||||
if (supportedPreviewSizes != null) {
|
||||
previewSize = getOptimalPreviewSize(supportedPreviewSizes, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user