我想在点击按钮时在前后摄像头之间切换。
当任何一个相机打开时,我需要释放它并打开另一个。
有人能告诉我要切换的代码块吗?
提前谢谢你。 。 。 。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/camera_btn"/>
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/front_back_btn"/>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Button01"
android:contentDescription="@string/app_name">
</ImageView>
<com.example.surfacecamera.CameraView
android:id="@+id/CameraView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>
这是我的活动
和主java类是
package com.example.surfacecamera;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
public class MainActivity extends Activity implements OnClickListener, Camera.PictureCallback {
CameraView cameraView;
ImageView imv;
Button b2 ;
int cameraCount = 0;
int camIdx = 0;
Camera cam;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
Button pictureButton = (Button) this.findViewById(R.id.Button01);
Button b2 = (Button) this.findViewById(R.id.Button02);
imv = (ImageView) this.findViewById(R.id.ImageView01);
cameraView = (CameraView) this.findViewById(R.id.CameraView01);
cameraCount = Camera.getNumberOfCameras();
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
pictureButton.setOnClickListener(this);
}
// From the OnClickListener
public void onClick(View v)
{
cameraView.takePicture(null, null, this);
}
// From the Camera.PictureCallback
public void onPictureTaken(byte[] data, Camera camera)
{
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imv.setImageBitmap(bmp);
String filename = "apicture.jpg";
File pictureFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + filename);
try
{
FileOutputStream pfos = new FileOutputStream(pictureFile);
bmp.compress(CompressFormat.JPEG, 75, pfos);
pfos.flush();
pfos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
和另一个班级
package com.example.surfacecamera;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraView extends SurfaceView implements SurfaceHolder.Callback
{
SurfaceHolder mHolder;
int width;
int height;
Camera mCamera;
public CameraView(Context context, AttributeSet attrs)
{
super(context,attrs);
holderCreation();
}
public CameraView(Context context)
{
super(context);
holderCreation();
}
public void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg)
{
mCamera.takePicture(shutter, raw, jpeg);
}
@SuppressWarnings("deprecation")
public void holderCreation()
{
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder)
{
// The Surface has been created, acquire the camera and tell it where to draw.
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
// If we aren't landscape (the default), tell the camera we want portrait mode
if (this.getResources().getConfiguration().orientation !=
Configuration.ORIENTATION_LANDSCAPE)
{
params.set("orientation", "portrait"); // "landscape"
// And Rotate the final picture if possible
// This works on 2.0 and higher only
//params.setRotation(90);
// Use reflection to see if it exists and to call it so you can support older versions
try {
Method rotateSet = Camera.Parameters.class.getMethod(
"setRotation", new Class[] { Integer.TYPE } );
Object arguments[] = new Object[] { new Integer(90) };
rotateSet.invoke(params, arguments);
} catch (NoSuchMethodException nsme) {
// Older Device
Log.v("CameraView","No Set Rotation");
} catch (IllegalArgumentException e) {
Log.v("CameraView","Exception IllegalArgument");
} catch (IllegalAccessException e) {
Log.v("CameraView","Illegal Access Exception");
} catch (InvocationTargetException e) {
Log.v("CameraView","Invocation Target Exception");
}
}
mCamera.setParameters(params);
try
{
mCamera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
width = w;
height = h;
// Now that the size is known, set up the camera parameters and begin the preview.
Camera.Parameters parameters = mCamera.getParameters();
//parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}