Search code examples
androidcamerasurfaceviewsurfaceholder

Android Camera - Half of the screen is black in Portrait mode


I was trying to create camera app, but I have faced this issue : when using camera in landscape mode everything is OK, but using in portrait with setDisplayOrientation(90) method brings up this issue:

Half of the screen is black.

Image here: https://i.sstatic.net/sPTex.jpg

surfaceCreated method:

@Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            this.camera = Camera.open();
            this.camera.setDisplayOrientation(90);
            this.camera.setPreviewDisplay(this.holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

My XML layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:id="@+id/cameraPreview"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
    </FrameLayout>
    <Button
        android:id="@+id/analyzeButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/analyze" />
</LinearLayout>

By the way, in screenshots taken with ADB - camera is shown normally. Tested on Xperia ST27i an Samsung Galaxy Pocket - same results.

Thank you


Solution

  • public void surfaceCreated(SurfaceHolder holder)
    {
    // The Surface has been created, acquire the camera and tell it where to draw.
    mCamera = Camera.open();
    
    Parameters params = mCamera.getParameters();
    
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
    {
    params.set("orientation", "portrait");
    mCamera.setDisplayOrientation(90);
    }
    
    try
    {
    mCamera.setPreviewDisplay(holder);
    }
    catch (IOException exception)
    {
    mCamera.release();
    mCamera = null;
    }
    
    }
    

    try these code friend ......................