首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要帮助来编码和使用android摄像头

我需要帮助来编码和使用android摄像头
EN

Stack Overflow用户
提问于 2012-05-11 23:00:04
回答 1查看 332关注 0票数 0

我想创建一个应用程序,允许过敏的人拍摄他们的反应,当他们有一些过敏反应,以及他们吃的食物。然后他们可以将信息添加到文档中,并保存所有这些信息,以及地理定位。(这个想法是,一个过敏的人需要一个日记,因为他通常每6个月可以看到一次他的过敏原)

我写了这样的代码:我必须点击手机的菜单按钮,这样我才能使用相机。但是,我现在想要有一个按钮,让我可以使用摄像头。有没有人可以帮我修改我的代码,帮我添加一个正确的按钮,这样我就可以用我的android手机拍照了?

忠实地,

我的代码:

打包android.camera;

代码语言:javascript
复制
        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.os.Bundle;
    import android.provider.MediaStore;
        import android.view.Menu;
        import android.view.MenuInflater;
        import android.view.MenuItem;
        import android.widget.ImageView;



        public class CameraActivity extends Activity {

            private static final int PICTURE_RESULT = 9;

            private Bitmap mPicture;
            private ImageView mView;

            public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                setContentView(R.layout.main);

                mView = (ImageView) findViewById(R.id.view);
            }

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

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.camera:
                    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    CameraActivity.this.startActivityForResult(camera, PICTURE_RESULT);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
                }
            }

    /*      public void onDestroy() {
                super.onDestroy();
                if (mPicture != null) {  
                    mPicture.recycle();
                }
            }*/

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                // if results comes from the camera activity
                if (requestCode == PICTURE_RESULT) {

                    // if a picture was taken
                    if (resultCode == Activity.RESULT_OK) {
                        // Free the data of the last picture
                        if(mPicture != null)
                            mPicture.recycle();

                        // Get the picture taken by the user
                        mPicture = (Bitmap) data.getExtras().get("data");

                        // Avoid IllegalStateException with Immutable bitmap 
                        Bitmap pic = mPicture.copy(mPicture.getConfig(), true);
                        mPicture.recycle();
                        mPicture = pic; 

                        // Show the picture
                        mView.setImageBitmap(mPicture);

                        // if user canceled from the camera activity
                    } else if (resultCode == Activity.RESULT_CANCELED) {

                    }
                }
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2012-05-12 01:37:58

我想这就是你要问的..。

在布局文件(main.xml)中添加一个按钮:

代码语言:javascript
复制
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:text="Take Picture" android:id="@+id/take_picture"></Button>

然后在你的onCreate方法中:

代码语言:javascript
复制
public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);

            mView = (ImageView) findViewById(R.id.view);
            mTakePictureButton = (Button) findViewById(R.id.take_picture);
            mTakePictureButton.setClickable(true);
            mTakePictureButton.setOnClickListener(new View.onClickListener(){

                    @Override
        public void onClick(View v) {
            Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(camera, PICTURE_RESULT);

        }
            });
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10553877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档