1. Download our Official Android App: Forums for Android!

Apps Camera Preview orientation Help!!!

Discussion in 'Android Development' started by S12, Jun 2, 2016.

  1. S12

    S12 Member
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    39
    Joined:
    Dec 24, 2015

    Dec 24, 2015
    39
    1
    16
    I'm Building a camera app and my Preview orientation is weird. I want it to be vertical but its showing landscape and its stretched out and skewed. I'm using a framelayout for the preview. http://i.stack.imgur.com/WNJUJ.png

    MainActivity
    Code (Text):
    1. import android.content.Context;
    2. import android.content.pm.PackageManager;
    3. import android.hardware.Camera;
    4. import android.support.v7.app.AppCompatActivity;
    5. import android.os.Bundle;
    6. import android.view.SurfaceView;
    7. import android.view.WindowManager;
    8. import android.widget.Button;
    9. import android.widget.FrameLayout;
    10. import android.widget.LinearLayout;
    11. import android.widget.RelativeLayout;
    12. import android.widget.Toast;
    13.  
    14. import java.io.File;
    15. import java.text.SimpleDateFormat;
    16. import java.util.Date;
    17. import java.io.FileNotFoundException;
    18. import java.io.FileOutputStream;
    19. import java.io.IOException;
    20. import android.hardware.Camera.PictureCallback;
    21. import android.view.View;
    22. import android.view.View.OnClickListener;
    23.  
    24. public class MainActivity extends AppCompatActivity {
    25.     private Camera mCamera;
    26.     private CameraPreview mPreview;
    27.     private Camera.PictureCallback mPicture;
    28.     private Button capture, switchCamera;
    29.     private Context myContext;
    30.     private FrameLayout cameraPreview;
    31.     private boolean cameraFront = false;
    32.     @Override
    33.     protected void onCreate(Bundle savedInstanceState) {
    34.         super.onCreate(savedInstanceState);
    35.         setContentView(R.layout.activity_main);
    36.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    37.         myContext = this;
    38.         initialize();
    39.     }
    40.     private int findFrontFacingCamera() {
    41.         int cameraId = -1;
    42.         // Search for the front facing camera
    43.         int numberOfCameras = Camera.getNumberOfCameras();
    44.         for (int i = 0; i < numberOfCameras; i++) {
    45.             Camera.CameraInfo info = new Camera.CameraInfo();
    46.             Camera.getCameraInfo(i, info);
    47.             if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    48.                 cameraId = i;
    49.                 cameraFront = true;
    50.                 break;
    51.             }
    52.         }
    53.         return cameraId;
    54.     }
    55.  
    56.     private int findBackFacingCamera() {
    57.         int cameraId = -1;
    58.         //Search for the back facing camera
    59.         //get the number of cameras
    60.         int numberOfCameras = Camera.getNumberOfCameras();
    61.         //for every camera check
    62.         for (int i = 0; i < numberOfCameras; i++) {
    63.             Camera.CameraInfo info = new Camera.CameraInfo();
    64.             Camera.getCameraInfo(i, info);
    65.             if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
    66.                 cameraId = i;
    67.                 cameraFront = false;
    68.                 break;
    69.             }
    70.         }
    71.         return cameraId;
    72.     }
    73.  
    74.     public void onResume() {
    75.         super.onResume();
    76.         if (!hasCamera(myContext)) {
    77.             Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
    78.             toast.show();
    79.             finish();
    80.         }
    81.         if (mCamera == null) {
    82.             //if the front facing camera does not exist
    83.             if (findFrontFacingCamera() < 0) {
    84.                 Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
    85.                 switchCamera.setVisibility(View.GONE);
    86.             }
    87.             mCamera = Camera.open(findBackFacingCamera());
    88.             mPicture = getPictureCallback();
    89.             mPreview.refreshCamera(mCamera);
    90.         }
    91.     }
    92.  
    93.     public void initialize() {
    94.         mPreview = new CameraPreview(myContext, mCamera);
    95.         cameraPreview = (FrameLayout) findViewById(R.id.camera_preview);
    96.         cameraPreview.addView(mPreview);
    97.  
    98.         capture = (Button) findViewById(R.id.button_capture);
    99.         capture.setOnClickListener(captrureListener);
    100.  
    101.         switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
    102.         switchCamera.setOnClickListener(switchCameraListener);
    103.     }
    104.  
    105.     OnClickListener switchCameraListener = new OnClickListener() {
    106.         @Override
    107.         public void onClick(View v) {
    108.             //get the number of cameras
    109.             int camerasNumber = Camera.getNumberOfCameras();
    110.             if (camerasNumber > 1) {
    111.                 //release the old camera instance
    112.                 //switch camera, from the front and the back and vice versa
    113.  
    114.                 releaseCamera();
    115.                 chooseCamera();
    116.             } else {
    117.                 Toast toast = Toast.makeText(myContext, "Sorry, your phone has only one camera!", Toast.LENGTH_LONG);
    118.                 toast.show();
    119.             }
    120.         }
    121.     };
    122.  
    123.     public void chooseCamera() {
    124.         //if the camera preview is the front
    125.         if (cameraFront) {
    126.             int cameraId = findBackFacingCamera();
    127.             if (cameraId >= 0) {
    128.                 //open the backFacingCamera
    129.                 //set a picture callback
    130.                 //refresh the preview
    131.  
    132.                 mCamera = Camera.open(cameraId);
    133.                 mPicture = getPictureCallback();
    134.                 mPreview.refreshCamera(mCamera);
    135.             }
    136.         } else {
    137.             int cameraId = findFrontFacingCamera();
    138.             if (cameraId >= 0) {
    139.                 //open the backFacingCamera
    140.                 //set a picture callback
    141.                 //refresh the preview
    142.  
    143.                 mCamera = Camera.open(cameraId);
    144.                 mPicture = getPictureCallback();
    145.                 mPreview.refreshCamera(mCamera);
    146.             }
    147.         }
    148.     }
    149.  
    150.     @Override
    151.     protected void onPause() {
    152.         super.onPause();
    153.         //when on Pause, release camera in order to be used from other applications
    154.         releaseCamera();
    155.     }
    156.  
    157.     private boolean hasCamera(Context context) {
    158.         //check if the device has camera
    159.         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    160.             return true;
    161.         } else {
    162.             return false;
    163.         }
    164.     }
    165.  
    166.     private PictureCallback getPictureCallback() {
    167.         PictureCallback picture = new PictureCallback() {
    168.  
    169.             @Override
    170.             public void onPictureTaken(byte[] data, Camera camera) {
    171.                 //make a new picture file
    172.                 File pictureFile = getOutputMediaFile();
    173.  
    174.                 if (pictureFile == null) {
    175.                     return;
    176.                 }
    177.                 try {
    178.                     //write the file
    179.                     FileOutputStream fos = new FileOutputStream(pictureFile);
    180.                     fos.write(data);
    181.                     fos.close();
    182.                     Toast toast = Toast.makeText(myContext, "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG);
    183.                     toast.show();
    184.  
    185.                 } catch (FileNotFoundException e) {
    186.                 } catch (IOException e) {
    187.                 }
    188.  
    189.                 //refresh camera to continue preview
    190.                 mPreview.refreshCamera(mCamera);
    191.             }
    192.         };
    193.         return picture;
    194.     }
    195.  
    196.     OnClickListener captrureListener = new OnClickListener() {
    197.         @Override
    198.         public void onClick(View v) {
    199.             mCamera.takePicture(null, null, mPicture);
    200.         }
    201.     };
    202.  
    203.     //make picture and save to a folder
    204.     private static File getOutputMediaFile() {
    205.         //make a new file directory inside the "sdcard" folder
    206.         File mediaStorageDir = new File("/sdcard/", "JCG Camera");
    207.  
    208.         //if this "JCGCamera folder does not exist
    209.         if (!mediaStorageDir.exists()) {
    210.             //if you cannot make this folder return
    211.             if (!mediaStorageDir.mkdirs()) {
    212.                 return null;
    213.             }
    214.         }
    215.  
    216.         //take the current timeStamp
    217.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    218.         File mediaFile;
    219.         //and make a media file:
    220.         mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    221.  
    222.         return mediaFile;
    223.     }
    224.  
    225.     private void releaseCamera() {
    226.         // stop and release camera
    227.         if (mCamera != null) {
    228.             mCamera.release();
    229.             mCamera = null;
    230.         }
    231.     }
    232. }
    CameraPreview

    Code (Text):
    1. import android.content.Context;
    2. import android.hardware.Camera;
    3. import android.util.Log;
    4. import android.view.SurfaceHolder;
    5. import android.view.SurfaceView;
    6.  
    7. import java.io.IOException;
    8.  
    9. public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
    10.     private SurfaceHolder mHolder;
    11.     private Camera mCamera;
    12.  
    13.     public CameraPreview(Context context, Camera camera) {
    14.         super(context);
    15.         mCamera = camera;
    16.         mHolder = getHolder();
    17.         mHolder.addCallback(this);
    18.         // deprecated setting, but required on Android versions prior to 3.0
    19.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    20.     }
    21.  
    22.     public void surfaceCreated(SurfaceHolder holder) {
    23.         try {
    24.             // create the surface and start camera preview
    25.             if (mCamera == null) {
    26.                 mCamera.setPreviewDisplay(holder);
    27.                 mCamera.startPreview();
    28.             }
    29.         } catch (IOException e) {
    30.             Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
    31.         }
    32.     }
    33.  
    34.     public void refreshCamera(Camera camera) {
    35.         if (mHolder.getSurface() == null) {
    36.             // preview surface does not exist
    37.             return;
    38.         }
    39.         // stop preview before making changes
    40.         try {
    41.             mCamera.stopPreview();
    42.         } catch (Exception e) {
    43.             // ignore: tried to stop a non-existent preview
    44.         }
    45.         // set preview size and make any resize, rotate or
    46.         // reformatting changes here
    47.         // start preview with new settings
    48.         setCamera(camera);
    49.         try {
    50.             mCamera.setPreviewDisplay(mHolder);
    51.             mCamera.startPreview();
    52.         } catch (Exception e) {
    53.             Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
    54.         }
    55.     }
    56.  
    57.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    58.         // If your preview can change or rotate, take care of those events here.
    59.         // Make sure to stop the preview before resizing or reformatting it.
    60.         refreshCamera(mCamera);
    61.     }
    62.  
    63.     public void setCamera(Camera camera) {
    64.         //method to set a camera instance
    65.         mCamera = camera;
    66.     }
    67.  
    68.     @Override
    69.     public void surfaceDestroyed(SurfaceHolder holder) {
    70.         // TODO Auto-generated method stub
    71.         // mCamera.release();
    72.  
    73.     }
    74. }

    AndroidManifest
    Code (Text):
    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    3.     <uses-permission android:name="android.permission.CAMERA" />
    4.     <uses-feature android:name="android.hardware.camera" />
    5.     <uses-feature android:name="android.hardware.camera.autofocus" />
    6.     <uses-feature android:name="android.hardware.camera.front" android:required="false" />
    7.  
    8.     <application
    9.         android:allowBackup="true"
    10.         android:icon="@mipmap/ic_launcher"
    11.         android:label="@string/app_name"
    12.         android:supportsRtl="true"
    13.         android:theme="@style/AppTheme">
    14.         <activity android:name=".MainActivity"
    15.             android:screenOrientation="portrait">
    16.             <intent-filter>
    17.                 <action android:name="android.intent.action.MAIN" />
    18.  
    19.                 <category android:name="android.intent.category.LAUNCHER" />
    20.             </intent-filter>
    21.         </activity>
    22.     </application>
    23.  
    24. </manifest>
    Activity_Main
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:tools="http://schemas.android.com/tools"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:orientation="horizontal"
    7.    >
    8.  
    9.         <FrameLayout
    10.             android:orientation="vertical"
    11.             android:id="@+id/camera_preview"
    12.             android:layout_width="match_parent"
    13.             android:layout_height="match_parent"
    14.             />
    15.  
    16.     <LinearLayout
    17.         android:id="@+id/buttonsLayout"
    18.         android:layout_width="match_parent"
    19.         android:layout_height="wrap_content"
    20.         android:layout_gravity="center"
    21.         android:orientation="horizontal"
    22.         android:layout_alignBottom="@+id/camera_preview">
    23.         <Button
    24.             android:id="@+id/button_ChangeCamera"
    25.             android:layout_width="72dp"
    26.             android:layout_height="wrap_content"
    27.             android:text="Switch Camera"
    28.             />
    29.         <Button
    30.             android:id="@+id/button_capture"
    31.             android:layout_width="wrap_content"
    32.             android:layout_height="wrap_content"
    33.             android:text="Capture"
    34.             />
    35.     </LinearLayout>
    36. </RelativeLayout>
     

    Advertisement

Share This Page

Loading...