这篇来学习一下如何调用摄像头拍照。
学习自《第一行代码》
以下内容皆为真机测试。
xml文件
就一个按钮和一个显示图片的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnTakePhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Take Photo"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
MainActivity中的代码
public class MainActivity extends AppCompatActivity {
private int takePhoto = 1;
private Uri imageUri;
private File outputImage;
private Button btnTakePhoto;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTakePhoto = findViewById(R.id.btnTakePhoto);
imageView