3

ボタンと画像ビューがあるアクティビティAがあります。ボタンをクリックすると、カメラが読み込まれ、画像ビューに画像が保存されます。誰でもこのコードを提供できますか。

前もって感謝します !!!

4

7 に答える 7

4

以下のコードを参照してください..それはあなたを助けるかもしれません.

この2行をボタンクリックに入れます..

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

コードでこのメソッドを作成します..

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

         if (requestCode == 0 && resultCode == RESULT_OK) {
            if (data != null) {

                photo = (Bitmap) data.getExtras().get("data");

                add_image.setImageBitmap(photo); /* this is image view where you want to set image*/

                Log.d("camera ---- > ", "" + data.getExtras().get("data"));


            }
        }
    }

................................................................... ...................................................

以下のコードは、カメラで取得した LastImageID を提供します。これは、穴の画像が必要な追加のコードにすぎません。

................................................................... ...................................................

private String getLastImageId() {
        final String[] imageColumns = { MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA };
        final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
        Cursor imageCursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
                null, null, imageOrderBy);
        if (imageCursor.moveToFirst()) {
            int id = imageCursor.getInt(imageCursor
                    .getColumnIndex(MediaStore.Images.Media._ID));
            String fullPath = imageCursor.getString(imageCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));

            imageCursor.close();
            return fullPath;
        } else {
            return "no path";
        }
    }
于 2012-07-04T07:22:01.997 に答える
1

次のように、画像をキャプチャしてボタンの onClickListener で呼び出すメソッドを作成できます。

   public void takePicture(View view){

    try{
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    } catch(Exception e){
        e.printStackTrace();
    }
}

そして、次のように onActivityResult メソッドをオーバーライドします。

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

    case CAMERA_PIC_REQUEST:

    String path=null;
            try{
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
                thumbnail = Bitmap.createScaledBitmap(thumbnail,yourDesiredImageWidth, yourDesiredImageHeight, true);
                ImageView imageView = (ImageView) findViewById(R.id.yourImageView);  
                image.setImageBitmap(thumbnail);
                break;
            }catch(Exception ee){
                ee.printStackTrace();
            }
        }
    }
于 2012-07-04T07:27:28.263 に答える
0

あなたの活動コード

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUREST = 1313;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnTakePhoto = (Button) findViewById(R.id.button1);
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

          if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {

                  if (data != null) {
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              imgTakenPhoto.setImageBitmap(thumbnail);
                  }
          }
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }
}

あなたのレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Press the button, capture photo and enjoy!!"
        android:id="@+id/textview1"/>

    <Button
        android:text="Take Photo!"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview1">
    </Button>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:src="@drawable/icon"
        android:scaleType="centerCrop"
        android:layout_below="@+id/button1">
    </ImageView>
</RelativeLayout>
于 2012-07-04T07:33:52.710 に答える
0

次のように試してください:

ステップ 1:カメラを起動して画像を撮影します。

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, 2); 

STEP 2:データonActivityResultから画像を取得

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            ImageView imageView=(ImageView) findViewbyId(R.id.imgv);
            imageView.setImageBitmap(photo);
        } 

編集:および2番目の解決策:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));  
 startActivityForResult(intent, 2);  




 @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == NONE)  
            return;  
        if (requestCode == 2) {   
            File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");  
            ImageView imgView=(ImageView)findViewById(R.id.imgView);
            Uri imgUri=Uri.fromFile(picture);
            imageView.setImageURI(imgUri);  
        }  
    }
于 2012-07-04T07:20:26.023 に答える
0

CemraImageActivity.java

public class CemraImageActivity extends Activity implements OnClickListener{
    private int CAMERA_REQUEST;
    private ImageView imageView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        this.imageView = (ImageView)this.findViewById(R.id.image_capture);
        imageView.setOnClickListener(this);           

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode != 0)
        {
            if (requestCode == CAMERA_REQUEST) 
            {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(photo);        
            }
        }
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    }  
}

main.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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="334dp"
        android:layout_height="wrap_content"
        android:text="Click this image for take a photo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/image_capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.06"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
于 2012-07-04T09:24:56.340 に答える
0

クリックボタンを取得してイメージビューにストアイメージを設定する非常に簡単な方法。

`public class MainActivity extends Activity {

private static final int CAMERA_PIC_REQUEST = 22;

Uri cameraUri;

Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath ;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImgPhoto = (ImageView) findViewById(R.id.imageView1);

    BtnSelectImage = (Button) findViewById(R.id.button1);
    BtnSelectImage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            try {
                   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                   startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
            }
        }
    });

}


@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
    try {
        switch (requestCode) {
       case CAMERA_PIC_REQUEST:
            if (resultCode == RESULT_OK) {
                try {
                      Bitmap photo = (Bitmap) data.getExtras().get("data"); 

                      ImgPhoto.setImageBitmap(photo);    

                } catch (Exception e) {
                    Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
                }
            }
            break;
          default:
            break;
        }
    } catch (Exception e) {
    }
}

}`

このマニフェストを設定します。

<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>

于 2014-07-19T07:15:24.387 に答える
0

マニフェストで必要な拳は、mainactivity.javaでこれを行った後にこれを行います

public class MainActivity extends Activity {
    Button button;
    ImageView photo;
    static final int Cam_Request = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.btnPhoto);
        photo = (ImageView)findViewById(R.id.ivPhoto);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent, Cam_Request);


            }
        });
    }
    private File getFile ()
    {
        File folder = new File("sdcard/camera_app");

        if(!folder.exists())
        {
            folder.mkdir();
        }

        File image = new File(folder,"image.jpg");
        return image;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/camera_app/image.jpg";
        photo.setImageDrawable(Drawable.createFromPath(path));
    }
}
于 2015-12-01T08:08:45.570 に答える