使用android单击将前摄像头翻转到后摄像头的按钮

使用android单击将前摄像头翻转到后摄像头的按钮,android,android-camera,Android,Android Camera,在安卓系统中,我面临着一个问题,就是在点击监听器按钮时从前向后翻转 这是我的密码 MainActivity.java package com.example.camerademo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; imp

在安卓系统中,我面临着一个问题,就是在点击监听器按钮时从前向后翻转

这是我的密码

MainActivity.java

package com.example.camerademo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;

public class CameraActivity extends Activity implements OnClickListener
{

    FrameLayout cameraView;
    SurfaceHolderActivity sHolder;
    Camera camera;
    ImageButton captureButton;
    File pictureFile;
    ImageView capturedImage1, capturedImage2, capturedImage3, flipcamera;
    int imagesCaptured = 0;
    String TAG = "Camera Application";
    Camera.CameraInfo cameraInfo;
    int cameraCount = 0;
    Camera cam = null;
    SurfaceHolder Surfaceholder;
    private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        camera = openFrontFacingCameraGingerbread();

        sHolder = new SurfaceHolderActivity(this, camera);

        // Surfaceholder = sHolder.previewHolder;

        cameraView = (FrameLayout) findViewById(R.id.cameraSurface);

        cameraView.addView(sHolder);

        captureButton = (ImageButton) findViewById(R.id.cameraCapture);
        captureButton.setOnClickListener(this);

        capturedImage1 = (ImageView) findViewById(R.id.capturedImage1);
        capturedImage2 = (ImageView) findViewById(R.id.capturedImage2);
        capturedImage3 = (ImageView) findViewById(R.id.capturedImage3);

        flipcamera = (ImageView) findViewById(R.id.FlipCamera);

        flipcamera.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                try
                {
                    flipcamera();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.camera, menu);
        return true;
    }

    @Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub
        switch (arg0.getId())
        {
            case R.id.cameraCapture:
                captureImage();
                break;

            default:
                break;
        }
    }

    PictureCallback pictureBack = new PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera)
        {
            pictureFile = getOutputMediaFile();
            // Log.d("hello", ""+pictureFile.toString());
            if (imagesCaptured != 3)
            {

                imagesCaptured++;
            }
            else
            {

                imagesCaptured = 1;
            }
            if (pictureFile == null)
            {
                return;
            }
            try
            {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            }
            catch (FileNotFoundException e)
            {

            }
            catch (IOException e)
            {
            }
            try
            {
                getImagePreview();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    private static File getOutputMediaFile()
    {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp");
        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;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".png");

        return mediaFile;
    }

    private void captureImage()
    {

        camera.takePicture(null, null, pictureBack);

        try
        {

            camera.reconnect();
            camera.startPreview();

        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void getImagePreview() throws IOException
    {
        System.out.println("image Path : " + pictureFile.getPath());
        // InputStream is = (InputStream) url.getContent();
        if (imagesCaptured == 1)
        {

            capturedImage1.setImageURI(Uri.parse(pictureFile.getPath()));

        }
        else if (imagesCaptured == 2)
        {
            capturedImage2.setImageURI(Uri.parse(pictureFile.getPath()));

        }
        else if (imagesCaptured == 3)
        {
            System.out.println("Path for the picture: " + Uri.parse(pictureFile.getPath()));
            System.out.println("capture " + capturedImage3);
            capturedImage3.setImageURI(Uri.parse(pictureFile.getPath()));
        }
    }

    private Camera openFrontFacingCameraGingerbread()
    {
        cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        System.out.println("count" +cameraCount);
        for (int camIdx = 0; camIdx < cameraCount; camIdx++)
        {

            Camera.getCameraInfo(camIdx, cameraInfo);

            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
            {
                try
                {
                    System.out.println("Camera No FRONT : " + camIdx);
                    cam = Camera.open(camIdx);
                }
                catch (RuntimeException e)
                {
                    Log.e(TAG,
                            "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
            else if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
                try
                {
                    System.out.println("Camera No bACK : " + camIdx);
                    cam = Camera.open(camIdx);
                }
                catch (RuntimeException e)
                {
                    Log.e(TAG,
                            "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }

        return cam;
    }

    public void flipcamera()
    {

        if (cameraCount > 0)
        {

            System.out.println("camera : " + camera);

            //camera.stopPreview();
            //camera.release();

            System.out.println("camera Object" + camera);

            if (camId == Camera.CameraInfo.CAMERA_FACING_BACK)
            {
                System.out.println("Back Camera");
                camera = Camera.open(1);
            }
            else
            {
                System.out.println("Front Camera");
                camera = Camera.open(0);
            }
            sHolder = new SurfaceHolderActivity(this, camera);
            cameraView.addView(sHolder);
            // setCameraDisplayOrientation(CameraActivity.this, camId, camera);

            // camera.startPreview();

        }
        else
        {
            camera = openFrontFacingCameraGingerbread();

            sHolder = new SurfaceHolderActivity(this, camera);

            // Surfaceholder = sHolder.previewHolder;

            // cameraView = (FrameLayout) findViewById(R.id.cameraSurface);

            cameraView.addView(sHolder);
        }
    }

}
Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.camerademo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/camera_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.camerademo.CameraActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果有人能解决我的问题,那就太好了


提前谢谢。

最后,我找到了问题的解决方案。这是我翻动相机的修改

private void flipcamera() {

        if (camera != null)
        {
            System.out.println("flipcamera");
            camera.stopPreview();
            camera.release();
            camera = null;

        }

        camera = Camera.open(camFront);

        if (camera != null) {
            try {
                camera.setPreviewDisplay(sHolder.getHolder());
                camera.startPreview();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

谢谢各位。

有什么问题吗斯加梅拉不是从前到后翻滚。它抛出错误05-31 00:07:36.541:W/System.err(14925):java.lang.RuntimeException:无法连接到摄影机服务05-31 00:07:36.541:W/CameraService(103):CameraService::connect X(pid 14925)已拒绝(现有客户端)。最后我找到了问题的解决方案。下面是我对翻转摄影机的修改,它可以翻转摄影机
private void flipcamera(){if(camera!=null){System.out.println(“flipcamera”);camera.stopReview();camera.release();camera=null;}camera=camera.open(camFront);if(camera!=null){try{camera.setPreviewDisplay(sHolder.getHolder());camera.startPreview();}catch(IOException e){//TODO自动生成的catch block e.printStackTrace();}}}
你可以分享你的xml布局“活动”\u camera吗?请参考我下面的帖子以获得答案。我已经得到了解决方案。谢谢Akanksha。
private void flipcamera() {

        if (camera != null)
        {
            System.out.println("flipcamera");
            camera.stopPreview();
            camera.release();
            camera = null;

        }

        camera = Camera.open(camFront);

        if (camera != null) {
            try {
                camera.setPreviewDisplay(sHolder.getHolder());
                camera.startPreview();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }