小视野中的前置摄像头侧面开口 - Android

Front Camera opening in the side in small view - Android

我正在我的代码中实现 android-gpuimage 库。 https://github.com/CyberAgent/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;

        }
    }
}

编辑: 我相机的屏幕截图:当我按下切换相机按钮时,后视图停止,我的前视图从角落开始。 我在其他设备上测试过。切换摄像头功能正在运行 perfectly.but,但在某些设备上无法正常运行。

 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

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();
    }

如果您遇到任何其他问题,请告诉我。