2

I am implementing android-gpuimage library in my code. https://github.com/CyberAgent/android-gpuimage I used the camera code of the sample project. Everyting works fine, but whenever I switch the camera, front camera opens in the side in a small view. I want the front camera to occupy the whole Surfaceview.

Here is my code:

    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;

        }
    }
}

EDIT: SCreenshot of my camera: when i press the switch camera button, the back view stops and my front view starts in the corner. i tested it in other device. The switchcamera feature is working perfectly.but, it is not working correctly in some devices.

enter image description here

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

Let me know if you face any further issue.

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

Set this for fill_parent and check.

Could you check this and paste screen

 <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" />

It supose to change size for 300x300

于 2016-04-28T06:58:05.163 回答