我在使用 onClickListener 按钮在 android 中从前向后翻转时遇到问题。
这是我的代码。
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
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;*/
}
}
}
清单.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>
它会ll be great, If anyone
解决我的问题。
提前致谢。