如何拍摄同一活动的注册表格

时间:2014-03-29 05:00:13

标签: android android-camera

我正在尝试创建一个Android应用程序,在其中我创建一个信息填充表单,用户填写信息。我还给了一个摄像头按钮捕获照片并保存。

我希望当用户点击相机按钮相机打开并拍照并保存时。

我没有打开相机按钮点击的另一个活动。而且我不想要相机的另一个xml布局文件。我想按相机按钮相机打开相同的布局并保存图片。

我该怎么做。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bl"
android:orientation="vertical"
android:weightSum="1.0" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content "
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight=".05"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginTop="8dp"
            android:layout_weight=".02"
            android:background="@drawable/keypad_select"
            android:orientation="horizontal"
            android:weightSum="1.0" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight=".50"
                android:text="FAULT TYPE:-"
                android:textColor="@color/orange"
                android:textSize="25sp" />

            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="0dp"
                android:layout_height="49dp"
                android:layout_weight="0.50" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginTop="8dp"
            android:layout_weight=".05"
            android:background="@drawable/keypad_select"
            android:orientation="horizontal"
            android:weightSum="1.0" >

            <TextView
                android:id="@+id/Button01"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".50"
                android:text="" />

            <Button
                android:id="@+id/save_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.19"
                android:background="@drawable/btn_white_glossy"
                android:text="SAVE.." />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:1)

用它来启动表格电话的相机应用程序

// called when the user selects to take a new picture 
public void take_Pic(final View view) {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);



}

然后在活动中:

/*
 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
 * the result for snapping a new picture or selecting one from the gallery 
 */

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

                  if (resultCode == RESULT_OK) {

                    myBitmap = data.getExtras().getParcelable("data");
                     ImageView photo = (ImageView) findViewById(R.id.pic);
                     photo.setImageBitmap(myBitmap);// here I am setting the pic to an image view for the user to have a look.  

                }

            }
     }

您不必为`Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE)制作新的类或布局;     startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);位。这只是打开手机或平板电脑的相机应用程序。

希望这有助于`

答案 1 :(得分:0)

在某些imageview中启动你的相机意图点击:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
 startActivityForResult(intent, CAPTURE_NEW_PICTURE);

然后在onActivityResult:

 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 mImage = (ImageView) findViewById(R.id.imageView1);
 mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));

这是您的decodeSampledBitmapFromFile

public static Bitmap decodeSampledBitmapFromFile(String path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
  }

上面的行解释如下:

1. When you click on image view, it start your camera intent. 
2. you can take photo using camera intent and save it.
3. Then it save into your SD card.
4. Finally you can retrieve your saved image from SD card and apply to your imageview.