Camera preview displays with correct aspect ratio.
This commit is contained in:
@@ -10,4 +10,11 @@
|
|||||||
android:paddingTop="@dimen/activity_vertical_margin"
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
tools:context=".CamActivity" >
|
tools:context=".CamActivity" >
|
||||||
|
|
||||||
</LinearLayout>
|
<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.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class CamActivity extends Activity{
|
public class CamActivity extends Activity{
|
||||||
@@ -33,9 +34,7 @@ public class CamActivity extends Activity{
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
|
setContentView(R.layout.activity_cam);
|
||||||
cPreview = new CameraPreview(this, hwCamera);
|
|
||||||
setContentView(cPreview);
|
|
||||||
|
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
serverIp = intent.getStringExtra("address");
|
serverIp = intent.getStringExtra("address");
|
||||||
@@ -82,9 +81,11 @@ public class CamActivity extends Activity{
|
|||||||
|
|
||||||
// TODO: pause the imThread and botThread objects.
|
// TODO: pause the imThread and botThread objects.
|
||||||
|
|
||||||
cPreview.removePreviewCallback();
|
if(cPreview != null){
|
||||||
cPreview.setCamera(null);
|
cPreview.removePreviewCallback();
|
||||||
releaseCamera();
|
cPreview.setCamera(null);
|
||||||
|
releaseCamera();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -99,7 +100,11 @@ public class CamActivity extends Activity{
|
|||||||
******************/
|
******************/
|
||||||
public void startCameraPreview(){
|
public void startCameraPreview(){
|
||||||
if(hwCamera != null){
|
if(hwCamera != null){
|
||||||
|
Logger.log_d(TAG, CLASS_NAME + ".startCameraPreview() :: Setting camera.");
|
||||||
|
cPreview = new CameraPreview(this, hwCamera);
|
||||||
cPreview.setCamera(hwCamera);
|
cPreview.setCamera(hwCamera);
|
||||||
|
((FrameLayout)findViewById(R.id.previewLayout)).addView(cPreview);
|
||||||
|
Logger.log_d(TAG, CLASS_NAME + ".startCameraPreview() :: Camera and content view set.");
|
||||||
}else{
|
}else{
|
||||||
Logger.log_wtf(TAG, CLASS_NAME + ".startCameraPreview() :: CAMERA IS NULL!");
|
Logger.log_wtf(TAG, CLASS_NAME + ".startCameraPreview() :: CAMERA IS NULL!");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
@@ -119,6 +124,7 @@ public class CamActivity extends Activity{
|
|||||||
@Override
|
@Override
|
||||||
protected Camera doInBackground(Void... params) {
|
protected Camera doInBackground(Void... params) {
|
||||||
Camera cam = null;
|
Camera cam = null;
|
||||||
|
Logger.log_d(TAG, CLASS_NAME + ".doInBackground() :: Opening the camera.");
|
||||||
try{
|
try{
|
||||||
cam = Camera.open(0);
|
cam = Camera.open(0);
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
|
@@ -13,20 +13,15 @@ import android.os.Build;
|
|||||||
import android.view.Surface;
|
import android.view.Surface;
|
||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
/** A basic Camera preview class */
|
/** A basic Camera preview class */
|
||||||
@SuppressLint("ViewConstructor")
|
@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 TAG = "SURFVIEW";
|
||||||
private final String CLASS_NAME = CameraPreview.class.getSimpleName();
|
private final String CLASS_NAME = CameraPreview.class.getSimpleName();
|
||||||
|
|
||||||
private Size previewSize;
|
|
||||||
private List<Size> supportedPreviewSizes;
|
|
||||||
private CameraImageMonitor imgMonitor;
|
private CameraImageMonitor imgMonitor;
|
||||||
private Activity parentActivity;
|
private Activity parentActivity;
|
||||||
private SurfaceView surfaceView;
|
|
||||||
private SurfaceHolder holder;
|
private SurfaceHolder holder;
|
||||||
private Camera camera;
|
private Camera camera;
|
||||||
|
|
||||||
@@ -35,8 +30,8 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
|
|||||||
super(context);
|
super(context);
|
||||||
parentActivity = (Activity)context;
|
parentActivity = (Activity)context;
|
||||||
|
|
||||||
surfaceView = new SurfaceView(context);
|
// surfaceView = new SurfaceView(context);
|
||||||
holder = surfaceView.getHolder();
|
holder = getHolder();
|
||||||
holder.addCallback(this);
|
holder.addCallback(this);
|
||||||
|
|
||||||
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB)
|
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){
|
public void setCamera(Camera camera){
|
||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
if(this.camera != null){
|
if(this.camera != null){
|
||||||
|
Logger.log_d(TAG, CLASS_NAME + ".setCamera() :: Setting camera.");
|
||||||
imgMonitor = CameraImageMonitor.getInstance();
|
imgMonitor = CameraImageMonitor.getInstance();
|
||||||
supportedPreviewSizes = this.camera.getParameters().getSupportedPreviewSizes();
|
|
||||||
requestLayout();
|
requestLayout();
|
||||||
|
Logger.log_d(TAG, CLASS_NAME + ".setCamera() :: Camera set.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder){
|
public void surfaceCreated(SurfaceHolder holder){
|
||||||
// The Surface has been created, now tell the camera where to draw the preview.
|
// 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 {
|
try {
|
||||||
if(camera != null)
|
if(camera != null)
|
||||||
camera.setPreviewDisplay(holder);
|
camera.setPreviewDisplay(holder);
|
||||||
} catch (IOException e) {
|
} 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();
|
requestLayout();
|
||||||
|
|
||||||
camParams = camera.getParameters();
|
camParams = camera.getParameters();
|
||||||
/*Size optimal = getOptimalPreviewSize(camParams.getSupportedPreviewSizes(), w, h);
|
Size optimal = getOptimalPreviewSize(camParams.getSupportedPreviewSizes(), w, h);
|
||||||
if(ProjectConstants.DEBUG)
|
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")");
|
||||||
Log.d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")");*/
|
camParams.setPreviewSize(optimal.width, optimal.height);
|
||||||
camParams.setPreviewSize(previewSize.width, previewSize.height);
|
|
||||||
camera.setParameters(camParams);
|
camera.setParameters(camParams);
|
||||||
|
|
||||||
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
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) {
|
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
|
||||||
final double ASPECT_TOLERANCE = 0.1;
|
final double ASPECT_TOLERANCE = 0.1;
|
||||||
double targetRatio = (double) w / h;
|
double targetRatio = (double) w / h;
|
||||||
|
|
||||||
|
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method started.");
|
||||||
if (sizes == null) return null;
|
if (sizes == null) return null;
|
||||||
|
|
||||||
Size optimalSize = 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;
|
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