Камера Side By Side с двумя 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.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 и удалив второе действие — средство запуска, мы можем избавиться от двух значков приложений на рабочем столе.

Но apk, сгенерированный таким образом при установке, открывается, показывая предварительный просмотр камеры только на левой поверхности, а правая поверхность выглядит черной,

Спасибо, надеюсь, вы, ребята, можете мне помочь :)


person Geeve George    schedule 27.03.2015    source источник
comment
Я в подобном квесте. Почему вы решили сделать 2 класса? Вы уже решили проблему?   -  person Niels    schedule 23.06.2015
comment
Эй, @Geeve Джордж и Нильс... вы оба добились этого... если да, то, пожалуйста, помогите мне... я также хочу, чтобы предварительный просмотр с нескольких камер выполнялся в одном действии.... я уже задавал вопрос здесь.. Пожалуйста, проверьте и скажите мне, знаете ли вы, что это срочно...... ПОЖАЛУЙСТА.... ссылка на мой вопрос.......stackoverflow.com/questions/41392791/   -  person Sagar Aghara    schedule 18.01.2017