กล้องเคียงข้างกันโดยใช้ SurfaceView สองเครื่อง

ฉันได้เขียนโค้ดสำหรับแอป Android ที่ควรแสดงตัวอย่างกล้องเคียงข้างกันในมุมมองพื้นผิวสองมุมมองพร้อมกัน

นี่คือผลลัพธ์ที่ฉันต้องการ:

http://i.stack.imgur.com/dBk3z.jpg

ปัญหาที่ฉันกำลังเผชิญ:

1) เมื่อติดตั้ง APK แล้ว จะมีการติดตั้งแอปสองแอป

2) แอปแรกจะแสดงตัวอย่างกล้องบนพื้นผิวด้านขวา

3) แอปที่สองแสดงตัวอย่างกล้องที่มุมมองพื้นผิวด้านซ้าย

ภาพหน้าจอ :

1) APK เดี่ยวติดตั้งสองแอป:

ลิงก์ไปยังภาพหน้าจอที่แสดงข้อผิดพลาด: http://i.stack.imgur.com/lKw2b.png< /ก>

2) เมื่อฉันเปิดแอปแรกจะแสดงตัวอย่างกล้องที่ SurfaceView ด้านซ้าย :

ลิงก์ไปยังภาพหน้าจอที่แสดงข้อผิดพลาด: i.stack.imgur.com/LqA5j.png

3) เมื่อฉันเปิดแอปที่สองจะแสดงตัวอย่างกล้องบน SurfaceView ด้านขวา :

ลิงก์ไปยังภาพหน้าจอที่แสดงข้อผิดพลาด: i.stack.imgur.com/DU6c7.png

รหัส :

Android Manifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.javacodegeeks.androidsurfaceviewexample.AndroidSurfaceviewExample"

        android:label="@string/app_name" 
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity
        android:name="com.javacodegeeks.androidsurfaceviewexample.AndroidSurfaceviewExample2"
        android:label="@string/app_name" 
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

    </activity>
</application>

First Java Class :

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;

PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    surfaceHolder = surfaceView.getHolder();

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    surfaceHolder.addCallback(this);

    // deprecated setting, but required on Android versions prior to 3.0


    jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Toast.makeText(getApplicationContext(), "Picture Saved", 2000).show();
            refreshCamera();
        }
    };
}

public void captureImage(View v) throws IOException {
    //take the picture
    camera.takePicture(null, null, jpegCallback);
}

public void refreshCamera() {
    if (surfaceHolder.getSurface() == null) {
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        camera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here
    // start preview with new settings
    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (Exception e) {

    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    refreshCamera();
}

public void surfaceCreated(SurfaceHolder holder) {
    try {
        // open the camera
        camera = Camera.open();
    } catch (RuntimeException e) {
        // check for exceptions
        System.err.println(e);
        return;
    }
    Camera.Parameters param;
    param = camera.getParameters();

    // modify parameter
    param.setPreviewSize(352, 288);
    camera.setParameters(param);
    try {
        // The Surface has been created, now tell the camera where to draw
        // the preview.
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (Exception e) {
        // check for exceptions
        System.err.println(e);
        return;
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // stop preview and release camera
    camera.stopPreview();
    camera.release();
    camera = null;
}

ชั้นสอง :

Camera camera;
SurfaceView surfaceView2;
SurfaceHolder surfaceHolder2;

PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    surfaceView2 = (SurfaceView) findViewById(R.id.surfaceView2);
    surfaceHolder2 = surfaceView2.getHolder();

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    surfaceHolder2.addCallback(this);

    // deprecated setting, but required on Android versions prior to 3.0


    jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Toast.makeText(getApplicationContext(), "Picture Saved", 2000).show();
            refreshCamera();
        }
    };
}

public void captureImage(View v) throws IOException {
    //take the picture
    camera.takePicture(null, null, jpegCallback);
}

public void refreshCamera() {
    if (surfaceHolder2.getSurface() == null) {
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        camera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here
    // start preview with new settings
    try {
        camera.setPreviewDisplay(surfaceHolder2);
        camera.startPreview();
    } catch (Exception e) {

    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    refreshCamera();
}

public void surfaceCreated(SurfaceHolder holder) {
    try {
        // open the camera
        camera = Camera.open();
    } catch (RuntimeException e) {
        // check for exceptions
        System.err.println(e);
        return;
    }
    Camera.Parameters param;
    param = camera.getParameters();

    // modify parameter
    param.setPreviewSize(352, 288);
    camera.setParameters(param);
    try {
        // The Surface has been created, now tell the camera where to draw
        // the preview.
        camera.setPreviewDisplay(surfaceHolder2);
        camera.startPreview();
    } catch (Exception e) {
        // check for exceptions
        System.err.println(e);
        return;
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // stop preview and release camera
    camera.stopPreview();
    camera.release();
    camera = null;
}

activity_main.xml :

    <SurfaceView
    android:id="@+id/surfaceView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />
    <SurfaceView
    android:id="@+id/surfaceView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />
</LinearLayout>

การอัปเดตที่สำคัญ :

ฉันพบว่าการแก้ไขไฟล์ AndroidManifest.xml และการลบกิจกรรมที่สองคือ Launcher ทำให้เราสามารถกำจัดไอคอนแอปทั้งสองในหน้าจอหลักได้

แต่ APK ที่สร้างขึ้นโดยการทำเช่นนี้ในการติดตั้งจะเปิดขึ้นโดยแสดงตัวอย่างกล้องบนมุมมองพื้นผิวด้านซ้ายและมุมมองพื้นผิวด้านขวาจะปรากฏเป็นสีดำ

ขอบคุณ หวังว่าพวกคุณจะช่วยฉันได้ :)


person Geeve George    schedule 27.03.2015    source แหล่งที่มา
comment
ฉันกำลังทำภารกิจที่คล้ายกัน ทำไมถึงตัดสินใจเรียน 2 คลาส? คุณได้แก้ไขปัญหาแล้วหรือยัง?   -  person Niels    schedule 23.06.2015
comment
เฮ้ @Geeve George และ Niels...คุณทั้งคู่ทำสิ่งนี้สำเร็จแล้ว...ถ้าใช่ โปรดช่วยฉันด้วย...ฉันยังต้องการให้แสดงตัวอย่างกล้องหลายตัวเป็นกิจกรรมเดียว....ฉันถามคำถามไปแล้วที่นี่..โปรดตรวจสอบและ บอกฉันถ้าคุณรู้ว่ามันเป็นเรื่องเร่งด่วน......กรุณา....คำถามของฉัน ลิงก์คือ.......stackoverflow.com/questions/41392791/   -  person Sagar Aghara    schedule 18.01.2017