攝像頭的應用(數值傳遞)
阿新 • • 發佈:2021-01-10
package com.androidstudy.uicomponenttest; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button;import android.widget.ImageView; public class CameraActivity extends AppCompatActivity { private Button btnCamera; private ImageView ivImage; private final int CAMERA_REQUEST = 10; //雙方認可,這個是標誌 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); initView(); } @Overrideprotected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode){ case CAMERA_REQUEST: if(resultCode == RESULT_OK){ Bitmap bitmap = (Bitmap) data.getExtras().get("data"); ivImage.setImageBitmap(bitmap); }break; } } public void initView(){ ivImage = findViewById(R.id.iv_capture_photo); btnCamera = findViewById(R.id.btn_camera_invoke); btnCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //啟動本機的攝像頭 //如何呼叫本機的攝像頭 ACTION是關鍵 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //啟動系統拍照程式,並將拍攝的照片返回顯示在ImageView元件中 startActivityForResult(intent,CAMERA_REQUEST); } }); } }