Fliping前置摄像头采用了android到相机背面的按钮单击 [英] Fliping the Front Camera to Back Camera in Button Click using android

查看:167
本文介绍了Fliping前置摄像头采用了android到相机背面的按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个问题,从正面翻转到后的机器人,在按钮onClickListener。

I am facing a problem with flipping the from front to back in android with in the button onClickListener.

下面是我的code。

Here is my code.

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

}

SurfaceHolderActivity.java

SurfaceHolderActivity.java

package com.example.camerademo;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceHolderActivity extends SurfaceView
        implements
            SurfaceHolder.Callback {

    Camera camera;
    SurfaceHolder previewHolder;

    public SurfaceHolderActivity(Context context, Camera camera) {
        super(context);
        // TODO Auto-generated constructor stub
        this.camera = camera;
        previewHolder = getHolder();
        previewHolder.addCallback(this);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch (IOException e) {
            // left blank for now
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.setPreviewCallback(null);
        try {
            if(camera == null){
            this.camera = Camera.open();
            }
            camera.setPreviewDisplay(holder);
            camera.startPreview();

        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if (camera != null) {
            System.out.println("called");
            camera.stopPreview();

            camera.setPreviewCallback(null);
            camera.release();
            //camera.release();
            //camera = null;
            /*camera.setPreviewCallback(null);
            camera.release();
            camera = null;*/
        }
    }

}

的Manifest.xml

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>

会是巨大的,如果有人会解决售后服务我的问题。

itll be great, If anyonell slove my problem.

在此先感谢。

推荐答案

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

Finally, i found the solution for the problem. Here is my modification to flip the camera

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

    }

谢谢你们。

这篇关于Fliping前置摄像头采用了android到相机背面的按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆