我想编写一个用于捕捉图片的Android相机程序。当我点击按钮时,我以这样的方式编写代码,它应该被捕获,但是注意到了。按钮单击发生,但控件不会出现在“onPictureTaken”上。我在这里粘贴我的代码:
package com.andr.sai.camerademo;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class camerademo extends Activity implements SurfaceHolder.Callback
{
/** Called when the activity is first created. */
SurfaceView mSurfaceView ;
SurfaceHolder mSurfaceHolder;
boolean mPreviewRunning;
Camera mCamera;
Bitmap bitmap = null;
Button b1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.camera_surface);
b1=(Button)findViewById(R.id.b1);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
System.out.println("111111111111111111111111");
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback()
{
public void onPictureTaken(byte[]imageData, Camera arg1) {
System.out.println("2222222222222222222222222222222222222");
BitmapFactory.Options opt = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, opt);
}
};
}
}
);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int arg1, int w, int h) {
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mCamera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}
public void onDraw(Canvas c)
{
c.drawColor(Color.WHITE);
c.drawBitmap( bitmap, 10, 10, null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="@+id/surface_camera"
android:layout_width="fill_parent"
android:layout_height="10dip"
android:layout_weight="1"
>
</SurfaceView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gettt"
android:id="@+id/b1"
></Button>
</LinearLayout>
在调试模式下,我运行了这个,我得到以下调试字符串:
08-24 17:43:41.293: ERROR/MediaPlayerService(30): error: -2
08-24 17:43:41.303: ERROR/MediaPlayer(30): Unable to to create media player
08-24 17:43:41.303: ERROR/CameraService(30): Failed to load CameraService sounds.
08-24 17:43:41.334: ERROR/MediaPlayerService(30): error: -2
08-24 17:43:41.344: ERROR/MediaPlayer(30): Unable to to create media player
08-24 17:43:41.344: ERROR/CameraService(30): Failed to load CameraService sounds.
08-24 17:43:41.354: DEBUG/CameraService(30): Client::Client X (pid 421)
08-24 17:43:41.364: DEBUG/CameraService(30): CameraService::connect X
08-24 17:43:41.364: DEBUG/CameraService(30): getParameters(picture-format=jpeg;picture-size=213x350;preview-format=yuv422sp;preview-frame-rate=15;preview-size=176x144)
08-24 17:43:41.445: DEBUG/CameraService(30): setParameters(preview-size=320x432;preview-format=yuv422sp;picture-size=213x350;picture-format=jpeg;preview-frame-rate=15)
08-24 17:43:41.445: DEBUG/CameraHardwareStub(30): initHeapLocked: preview size=320x432
08-24 17:43:41.455: DEBUG/CameraService(30): setPreviewDisplay(0xe1f0) (pid 421)
08-24 17:43:41.464: DEBUG/CameraService(30): startPreview (pid 421)
08-24 17:43:41.464: DEBUG/CameraService(30): startCameraMode(0) (pid 421)
08-24 17:43:41.464: DEBUG/CameraService(30): startPreviewMode (pid 421)
你可以帮我看看如何在Android中运行Sample camera程序来拍照。
答案 0 :(得分:2)
我没有看到打电话 takePicture(Camera.ShutterCallback,Camera.PictureCallback,Camera.PictureCallback,Camera.PictureCallback)。
在onclick中添加Camera.takepicture
答案 1 :(得分:0)
package makemachine.android.examples;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class PhotoCaptureExample extends Activity
{
protected Button _button;
protected ImageView _image;
protected TextView _field;
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_image = ( ImageView ) findViewById( R.id.image );
_field = ( TextView ) findViewById( R.id.field );
_button = ( Button ) findViewById( R.id.button );
_button.setOnClickListener( new ButtonClickHandler() );
_path = Environment.getExternalStorageDirectory() + "/images /make_machine_example.jpg";
}
public class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
startCameraActivity();
}
}
protected void startCameraActivity()
{
Log.i("MakeMachine", "startCameraActivity()" );
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i( "MakeMachine", "resultCode: " + resultCode );
switch( resultCode )
{
case 0:
Log.i( "MakeMachine", "User cancelled" );
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
Log.i( "MakeMachine", "onPhotoTaken" );
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile( _path, options );
_image.setImageBitmap(bitmap);
_field.setVisibility( View.GONE );
}
@Override
protected void onRestoreInstanceState( Bundle savedInstanceState){
Log.i( "MakeMachine", "onRestoreInstanceState()");
if( savedInstanceState.getBoolean( PhotoCaptureExample.PHOTO_TAKEN ) ) {
onPhotoTaken();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_CAMERA:
Toast.makeText(PhotoCaptureExample.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_1:
Toast.makeText(PhotoCaptureExample.this, "Pressed 1", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_HOME:
Toast.makeText(PhotoCaptureExample.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_BACK:
Toast.makeText(PhotoCaptureExample.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
Intent result = new Intent("Complete");
setResult(Activity.RESULT_OK, result);
finish();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putBoolean( PhotoCaptureExample.PHOTO_TAKEN, _taken );
}
}