亲宝软件园·资讯

展开

android实现拍照功能 android实现手机App实现拍照功能示例

ly593988490 人气:0
想了解android实现手机App实现拍照功能示例的相关内容吗,ly593988490在本文为您仔细讲解android实现拍照功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android实现拍照功能,android,实现拍照,android拍照功能,下面大家一起来学习吧。

实现手机App实现拍照功能结果如下


第一步:

activity_takephoto.xml布局用SurfaceView

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_takephoto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.android_27.TakephotoActivity">
      <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_takephoto"
        />
  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="拍照"
      android:id="@+id/b_OK"
      />
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="切换"
      android:id="@+id/b_qh"
      />
  </LinearLayout>
</RelativeLayout>

第二步:TakephotoActivity类

public class TakephotoActivity extends AppCompatActivity {

  private SurfaceView sv_takephoto;
  private Button b_OK;
  private Button b_qh;
  private Camera camera;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_takephoto);
    //获得SurfaceView的ID
    sv_takephoto =(SurfaceView) findViewById(R.id.sv_takephoto);
    //获得拍照的ID
    b_OK =(Button)findViewById(R.id.b_OK);
    //获得切换镜头的ID
    b_qh =(Button)findViewById(R.id.b_qh);

    sv_takephoto.getHolder().addCallback(new SurfaceHolder.Callback() {
      @Override
      public void surfaceCreated(SurfaceHolder surfaceHolder) {
      //打开相机
        camera = Camera.open();
        //给相机设置参数
        Camera.Parameters parameters= camera.getParameters();
        //设置保存的格式
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.set("jpeg-quality",85);
        camera.setParameters(parameters);

        //将画面展示到SurfaceView中
        try {
          camera.setPreviewDisplay(sv_takephoto.getHolder());
          //开启预览
          camera.startPreview();
        } catch (IOException e) {
          e.printStackTrace();
        }

      }

      @Override
      public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

      }

      @Override
      public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

      }
    });

    //拍照的点击事件
    b_OK.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //拍照
        camera.takePicture(null, null, new Camera.PictureCallback() {
          @Override
          public void onPictureTaken(byte[] bytes, Camera camera) {
            //将字节数组转成图片
            Bitmap bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
            try {
              FileOutputStream fos=new FileOutputStream("/mnt/sdcard/DCIM/"+System.currentTimeMillis()+".jpg");
              bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);
              camera.stopPreview();
              camera.startPreview();
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            }
          }
        });

    }
    });

  }
}

第三步:在AndroidManifest.xml中加权限

<uses-permission android:name="android.permission.CAMERA" />

加载全部内容

相关教程
猜你喜欢
用户评论