resize the preview size as the ratio of picture size
authorPeng Li <seudut@gmail.com>
Fri, 25 May 2018 06:36:57 +0000 (14:36 +0800)
committerPeng Li <seudut@gmail.com>
Fri, 25 May 2018 06:36:57 +0000 (14:36 +0800)
app/src/main/java/ai/suanzi/rtmpclient/CameraView.java
app/src/main/java/ai/suanzi/rtmpclient/MainActivity.java
app/src/main/java/ai/suanzi/rtmpclient/MyService.java
app/src/main/res/layout/activity_main.xml

index 5ddcb5e..71a684d 100644 (file)
@@ -3,6 +3,7 @@ package ai.suanzi.rtmpclient;
 import android.content.Context;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
+import android.widget.FrameLayout;
 
 import org.apache.log4j.Logger;
 
@@ -15,6 +16,8 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
     private Context mContext;
     private SurfaceHolder mHolder;
     private Callback mCallback;
+    private int width;
+    private int height;
 
     public CameraView (Context context, Callback cb) {
         super(context);
@@ -24,10 +27,32 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
         this.mCallback = cb;
     }
 
+    public void setLayout(int width, int heitht){
+        this.height = heitht;
+        this.width = width;
+        gLogger.error("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
+
+        float ratio = (float) height / width; // 3/4,,,  640x480  1008x1344
+        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)getLayoutParams();
+        gLogger.error("ratio = " + ratio + ". xxx=" + (float)(SCREEN_HEIGHT / SCREEN_HEIGHT));
+        if(ratio > (float)(SCREEN_HEIGHT / SCREEN_WIDTH) ){
+            lp.width = SCREEN_HEIGHT * width / height;
+            lp.height = SCREEN_HEIGHT;
+            lp.leftMargin = (SCREEN_WIDTH - lp.width ) / 2;
+            lp.topMargin = 0;
+        } else {
+            lp.width = SCREEN_WIDTH;
+            lp.height = SCREEN_WIDTH * height / width;
+            lp.leftMargin = 0;
+            lp.topMargin = (SCREEN_HEIGHT - lp.height) / 2;
+        }
+        this.setLayoutParams(lp);
+    }
+
     // SurfaceHolder.Callback implementation
     @Override
     public void surfaceCreated(final SurfaceHolder holder){
-        gLogger.error("SurfacedCreated");
+        gLogger.error("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
     }
 
     @Override
index 685d85d..16c39d9 100644 (file)
@@ -1,6 +1,7 @@
 package ai.suanzi.rtmpclient;
 
 import android.content.IntentFilter;
+import android.hardware.Camera;
 import android.support.design.widget.TextInputEditText;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
@@ -29,12 +30,13 @@ import android.content.ComponentName;
 import ai.suanzi.rtmpclient.MyService.LocalBinder;
 import android.os.IBinder;
 import android.net.ConnectivityManager;
+import android.view.ViewGroup;
 
 //"rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90";
 
 public class MainActivity extends AppCompatActivity implements MyService.MyServiceEventListener, CameraView.Callback {
 
-    private static final String TAG = "MainActivity";
+    //private static final String TAG = "MainActivity";
     private Logger gLogger;
 
     private String mMacAddr = "";
@@ -54,20 +56,20 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
             final LogConfigurator logConfigurator = new LogConfigurator();
             String fname = getExternalFilesDir(null) + File.separator + "log.txt";
             logConfigurator.setFileName(fname);
-            Log.e(TAG, "Log file is located at: " + fname);
             logConfigurator.setRootLevel(Level.DEBUG);
             logConfigurator.setLevel("org.apache", Level.ERROR);
             logConfigurator.setMaxFileSize(1024 * 1024 * 10);
             logConfigurator.configure();
             gLogger = Logger.getLogger(getClass());
+            gLogger.error("#######################################");
+            gLogger.debug("Log file is located at: " + fname);
+
         } catch (Exception e){
             e.printStackTrace();
         }
     }
 
     private void init(){
-        configLog();
-        gLogger.debug("#######################################");
         // set config file
         UserInfo.setConfigPath(getExternalFilesDir(null) + File.separator + "config");
 
@@ -153,6 +155,8 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
             if(mServer.setRtmpUrl(UserInfo.getConfig().toUrl(mMacAddr))){
                 //mServer.startPreview(mHolder);
                 mServer.startPreview(mCameraView.getHolder());
+                Camera.Size cs = mServer.getBestPictureSize();
+                mCameraView.setLayout(cs.width, cs.height);
             }
 
 
@@ -182,26 +186,31 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         ///setContentView(R.layout.activity_main);
 
-
-        DisplayMetrics outMetrics = new DisplayMetrics();
-        this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
-
         ActionBar actionBar = getSupportActionBar();
         actionBar.hide();
 
+        DisplayMetrics outMetrics = new DisplayMetrics();
         this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
 
         CameraView.SCREEN_WIDTH = outMetrics.widthPixels;
         CameraView.SCREEN_HEIGHT = outMetrics.heightPixels;
+        gLogger.debug("Screen size is w: " + CameraView.SCREEN_WIDTH  + ", h: " + CameraView.SCREEN_HEIGHT);
+
+
+
         mCameraView = new CameraView(getApplicationContext(), this);
-        setContentView(mCameraView);
+        setContentView(R.layout.activity_main);
+
+        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(640, 480);
+        addContentView(mCameraView, lp);
+        //setContentView(mCameraView);
     }
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        configLog();
+        //gLogger.debug("#######################################");
         initCameraView();
-
-
         init();
         loadConfig();
 
@@ -301,9 +310,9 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
     @Override
     public void onCameraError(String msg){
         gLogger.error("onCameraEvent " + msg);
-        if(mUsbMonitor.hasUsbCamera()){
+        //if(mUsbMonitor.hasUsbCamera()){
             //mServer.reopenCamera();
-        }
+        //}
     }
 
     @Override
index 6b7ecf8..ff2c182 100644 (file)
@@ -25,8 +25,7 @@ public class MyService extends Service  implements Camera.PreviewCallback, Camer
     private long frameCount = 0;
 
     // Preferred picture Size of the camera;
-    private int width = 0;
-    private int height = 0;
+    private Camera.Size mPreferredSize;
 
 
     private MyServiceEventListener mListener;
@@ -194,6 +193,10 @@ public class MyService extends Service  implements Camera.PreviewCallback, Camer
 
     public void startPreview (SurfaceHolder holder){
         gLogger.debug("startPreview");
+        if(mCamera == null){
+            gLogger.error("startPreview - error: camera is null");
+            return;
+        }
         try {
             mCamera.setPreviewDisplay(holder);
             mCamera.startPreview();
@@ -216,12 +219,12 @@ public class MyService extends Service  implements Camera.PreviewCallback, Camer
 
     public boolean setRtmpUrl (String url){
         //this.rtmpUrl = url;
-        if(mCamera == null){
-            gLogger.error("setRtmpUrl, error mCamera is null");
+        if(mCamera == null || mPreferredSize == null){
+            gLogger.error("setRtmpUrl, error mCamera or PreferredSize is null");
             return false;
         }
-        gLogger.error("setRtmpUrl - size: " +  width + "x" + height + ". url: " + url);
-        int ret = FfmpegHelper.initEncoder(width, height, url);
+        gLogger.error("setRtmpUrl - size: " +  mPreferredSize.width + "x" + mPreferredSize.height + ". url: " + url);
+        int ret = FfmpegHelper.initEncoder(mPreferredSize.width, mPreferredSize.height, url);
         if(ret != 0){
             gLogger.error("setRtmpUrl, initEncoder error");
         }
@@ -248,8 +251,7 @@ public class MyService extends Service  implements Camera.PreviewCallback, Camer
         for(int[] i : paras.getSupportedPreviewFpsRange()){
             gLogger.error("[" + i[0] + "," + i[1] + "]");
         }
-        width = preferredSize.width;
-        height = preferredSize.height;
+        mPreferredSize = preferredSize;
         paras.setPictureSize(preferredSize.width, preferredSize.height); // use 640x480 preferred
         camera.setParameters(paras);
         camera.setDisplayOrientation(0);
@@ -258,6 +260,10 @@ public class MyService extends Service  implements Camera.PreviewCallback, Camer
         camera.setPreviewCallback(this);
     }
 
+    public Camera.Size getBestPictureSize(){
+        return mPreferredSize;
+    }
+
     // Camaer.onError callback
     @Override
     public void onError(int error, Camera camera){
index b59b0fb..09f0c78 100644 (file)
@@ -4,84 +4,7 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:background="@android:color/background_dark"
     tools:context=".MainActivity">
 
-    <Button
-        android:id="@+id/button"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginBottom="18dp"
-        android:layout_marginEnd="149dp"
-        android:layout_marginStart="147dp"
-        android:text="@string/btn"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent" />
-
-    <SurfaceView
-        android:id="@+id/surfaceView"
-        android:layout_width="335dp"
-        android:layout_height="178dp"
-        android:layout_marginEnd="5dp"
-        android:layout_marginStart="5dp"
-        android:layout_marginTop="248dp"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintHorizontal_bias="0.512"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
-
-    <android.support.design.widget.TextInputLayout
-        android:id="@+id/textInputLayout"
-        android:layout_width="0dp"
-        android:layout_height="59dp"
-        android:layout_marginEnd="8dp"
-        android:layout_marginStart="8dp"
-        android:layout_marginTop="16dp"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent">
-
-        <android.support.design.widget.TextInputEditText
-            android:id="@+id/textServer"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:hint="@string/labelServer"
-            android:text="@string/rtmpServer" />
-    </android.support.design.widget.TextInputLayout>
-
-    <android.support.design.widget.TextInputLayout
-        android:id="@+id/textInputLayout2"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:layout_margin="5dp"
-        android:layout_marginEnd="8dp"
-        android:layout_marginStart="8dp"
-        android:layout_marginTop="15dp"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
-        app:layout_goneMarginTop="10dp">
-
-        <android.support.design.widget.TextInputEditText
-            android:id="@+id/textUser"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:hint="@string/labelUser"
-            android:text="@string/user" />
-    </android.support.design.widget.TextInputLayout>
-
-    <android.support.design.widget.TextInputLayout
-        android:id="@+id/textInputLayout3"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:layout_margin="5dp"
-        android:layout_marginEnd="8dp"
-        android:layout_marginStart="8dp"
-        android:layout_marginTop="18dp"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">
-
-    </android.support.design.widget.TextInputLayout>
-
 </android.support.constraint.ConstraintLayout>
\ No newline at end of file