Android 简单调用摄像头
IDE: android studio3.1.2
界面:
activity_main2.xml
代码
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main2Activity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:orientation="vertical" app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main2Activity"> <TextView android:layout_width="wrap_content" android:layout_height="80dp" android:gravity="center" android:layout_gravity="center" android:text="" /> <ImageView android:id="@+id/imageIV" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" /> <Button android:id="@+id/takephotoTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="开启相机" /> </LinearLayout> </LinearLayout> </android.support.constraint.ConstraintLayout>
package com.example.mark.demo01; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class Main2Activity extends AppCompatActivity { @BindView(R.id.takephotoTV) Button takephotoTV; @BindView(R.id.imageIV) ImageView imageIV; private final int CAMERA_REQUEST = 8888; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ButterKnife.bind(this); } @OnClick(R.id.takephotoTV) public void onClick() { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageIV.setImageBitmap(photo); } } }
从课设中挑出来的模块,可能会有点小错,无伤大雅