2

コードに android-gpuimage ライブラリを実装しています。 https://github.com/Cyber​​Agent/android-gpuimage サンプルプロジェクトのカメラコードを使用しました。すべて正常に動作しますが、カメラを切り替えるたびに、前面カメラが小さなビューで側面に開きます。Surfaceview全体をフロントカメラで占めたい。

これが私のコードです:

    public class CameraActivity extends Activity implements OnSeekBarChangeListener, OnClickListener {

    private GPUImage mGPUImage;
    private CameraHelper mCameraHelper;
    private CameraLoader mCamera;
    private GPUImageFilter mFilter;
    private int cameraId;
    private FilterAdjuster mFilterAdjuster;
    static int camerastate=0;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        ((SeekBar) findViewById(R.id.seekBar)).setOnSeekBarChangeListener(this);
        findViewById(R.id.filter).setOnClickListener(this);
        findViewById(R.id.flipCamera).setOnClickListener(this);
        findViewById(R.id.captureImage).setOnClickListener(this);
        cameraId = CameraInfo.CAMERA_FACING_BACK;
        mGPUImage = new GPUImage(this);
        mGPUImage.setImage(mImage);
        mGPUImage.setFilter(mGPUImageFilter);
        mGPUImage.getBitmapWithFilterApplied();*/
        mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));


        mCameraHelper = new CameraHelper(this);
        mCamera = new CameraLoader();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mCamera.onResume();
    }

    @Override
    protected void onPause() {
        mCamera.onPause();
        super.onPause();
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.filter:
                GPUImageFilterTools.showDialog(this, new OnGpuImageFilterChosenListener() {

                    @Override
                    public void onGpuImageFilterChosenListener(final GPUImageFilter filter) {
                        switchFilterTo(filter);
                    }
                });
                break;

            case R.id.captureImage:
                if (mCamera.mCameraInstance.getParameters().getFocusMode().equals(
                        Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
                    takePicture();
                } else {
                    mCamera.mCameraInstance.autoFocus(new Camera.AutoFocusCallback() {

                        @Override
                        public void onAutoFocus(final boolean success, final Camera camera) {
                            takePicture();
                        }
                    });
                }
                break;

            case R.id.flipCamera:
                mCamera.switchCamera();
                break;
        }
    }

    private void takePicture() {
        // TODO get a size that is about the size of the screen
        Camera.Parameters params = mCamera.mCameraInstance.getParameters();
        params.setRotation(90);
        mCamera.mCameraInstance.setParameters(params);
        for (Camera.Size size : params.getSupportedPictureSizes()) {
            Log.i("ASDF", "Supported: " + size.width + "x" + size.height);
        }
        mCamera.mCameraInstance.takePicture(null, null,
                new Camera.PictureCallback() {

                    @Override
                    public void onPictureTaken(byte[] data, final Camera camera) {

                        final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                        if (pictureFile == null) {
                            Log.d("ASDF",
                                    "Error creating media file, check storage permissions");
                            return;
                        }

                        try {
                            FileOutputStream fos = new FileOutputStream(pictureFile);
                            fos.write(data);
                            fos.close();
                        } catch (FileNotFoundException e) {
                            Log.d("ASDF", "File not found: " + e.getMessage());
                        } catch (IOException e) {
                            Log.d("ASDF", "Error accessing file: " + e.getMessage());
                        }

                        data = null;
                        Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());


                        mGPUImage1.setImage(pictureFile);
                        mGPUImage1.setFilter(new GPUImageSepiaFilter());



                        final GLSurfaceView view = (GLSurfaceView) findViewById(R.id.surfaceView);
                        view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
                 mGPUImage1.saveToPictures(bitmap1, "GPUImage",
                                System.currentTimeMillis() + ".jpg",
                                new OnPictureSavedListener() {

                                    @Override
                                    public void onPictureSaved(final Uri
                                                                       uri) {
                                           pictureFile.delete();
                                        camera.startPreview();
                                        view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
                                    }
                                });
                        Log.e("Activity", "GPUIMAGE " + mGPUImage.toString());
                    }
                });
    }

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    private static File getOutputMediaFile(final int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    private void switchFilterTo(final GPUImageFilter filter) {
        if (mFilter == null
                || (filter != null && !mFilter.getClass().equals(filter.getClass()))) {
            mFilter = filter;
            mGPUImage.setFilter(mFilter);
            mFilterAdjuster = new FilterAdjuster(mFilter);
        }
    }

    @Override
    public void onProgressChanged(final SeekBar seekBar, final int progress,
            final boolean fromUser) {
        if (mFilterAdjuster != null) {
            mFilterAdjuster.adjust(progress);
        }
    }

    @Override
    public void onStartTrackingTouch(final SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(final SeekBar seekBar) {
    }

    private class CameraLoader {
        int mCurrentCameraId = 0;
        Camera mCameraInstance;



        public void onResume() {
            setUpCamera(mCurrentCameraId);
        }

        public void onPause() {
            releaseCamera();
        }

        public void switchCamera() {
           releaseCamera();

            mCurrentCameraId = (mCurrentCameraId + 1) % mCameraHelper.getNumberOfCameras();

            setUpCamera(mCurrentCameraId);
        }

        private void setUpCamera(int id) {
            Log.e("Activity", "ID1 " + id);
            mCameraInstance = getCameraInstance(id);
            Camera.Parameters parameters = mCameraInstance.getParameters();
            // TODO adjust by getting supportedPreviewSizes and then choosing
            // the best one for screen size (best fill screen)
            if (parameters.getSupportedFocusModes().contains(
                    Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
                parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            }
            parameters.setPictureSize(640,480);

            mCameraInstance.setParameters(parameters);

            int orientation = mCameraHelper.getCameraDisplayOrientation(
                    CameraActivity.this, mCurrentCameraId);
            CameraInfo2 cameraInfo = new CameraInfo2();

            mCameraHelper.getCameraInfo(mCurrentCameraId, cameraInfo);
            boolean flipHorizontal = cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT;

 mGPUImage.setUpCamera(mCameraInstance, orientation, flipHorizontal, false);

        }

        /** A safe way to get an instance of the Camera object. */
        private Camera getCameraInstance(int id) {
            Camera c = null;
            Log.e("Activity","Camera Instance " + id);
            try {

                    c = mCameraHelper.openCamera(id);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return c;
        }

        private void releaseCamera() {


            mCameraInstance.setPreviewCallback(null);
            mCameraInstance.stopPreview();
            mCameraInstance.release();
            mCameraInstance= null;

        }
    }
}

編集: カメラのスクリーンショット: カメラの切り替えボタンを押すと、背面ビューが停止し、正面ビューが隅から始まります。他のデバイスでテストしました。スイッチカメラ機能は問題なく動作していますが、一部のデバイスでは正しく動作していません。

ここに画像の説明を入力

4

2 に答える 2

4
This will surely fix your issue : 

**How to switch**
 public void switchCamera() {
            releaseCamera();
            mCameraId = (mCameraId + 1) % Camera.getNumberOfCameras();
            **mGpuImageView.reInitLayout();**//the trick
            prepareCamera();
    }

**releaseCamera**
private void releaseCamera() {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }

**prepareCamera**
your camera prepare method


**mGpuImageView.reInitLayout**
//Put this inside your GPUImageView.java class

 public void reInitLayout() {
        mGLSurfaceView.requestLayout();
        mGPUImage.deleteImage();
    }

さらに問題が発生した場合はお知らせください。

于 2016-05-01T17:26:16.257 に答える
1
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"

これを fill_parent に設定してチェックします。

これを確認して画面を貼り付けていただけますか

 <ImageView
        android:id="@+id/img_switch_camera"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:layout_weight="0.08"
        android:src="@drawable/ic_switch_camera" />

300x300のサイズを変更することを想定しています

于 2016-04-28T06:58:05.163 に答える