欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之Camera解析

程序员文章站 2022-03-09 22:37:09
android开发之camera解析 androidmanifest.xml activity_main.x...

android开发之camera解析

Android开发之Camera解析

Android开发之Camera解析

androidmanifest.xml



 
 

 
  

 

 

  
 

activity_main.xml




 <framelayout android:id="@+id/cameralayout" android:layout_gravity="center_horizontal" android:layout_height="300dp" android:layout_width="300dp">

 

</framelayout>

mainactivty.java

package com.hala.camera;

import android.content.context;
import android.content.intent;
import android.hardware.camera;
import android.net.uri;
import android.os.environment;
import android.provider.mediastore;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.surfaceholder;
import android.view.surfaceview;
import android.view.view;
import android.widget.framelayout;
import android.widget.imageview;

import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;

public class mainactivity extends appcompatactivity {
 public static final string file_path = environment.getexternalstoragedirectory().getpath() + "/imooc/temp.jpg";
 public static final int request_code = 0;
 private imageview mimageview;
 private framelayout mcameralayout;
 private camera mcamera;
 private cameraview mcameraview;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  mimageview = (imageview) findviewbyid(r.id.imageview);
  mcameralayout = (framelayout) findviewbyid(r.id.cameralayout);
 }

 public void systemcamera(view view) {

  if (mcameraview != null) {
mcameraview.release();
  }
  // open system camera.

  intent intent = new intent();
  intent.setaction(mediastore.action_image_capture);
  intent.addcategory(intent.category_default);

  file file = new file(file_path);
  if (file.exists()) {
file.delete();
  }

  // 文件转换成uri
  uri uri = uri.fromfile(file);
  intent.putextra(mediastore.extra_output, uri);

  startactivityforresult(intent, request_code);
 }

 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  super.onactivityresult(requestcode, resultcode, data);
  if (requestcode == request_code) {
file file = new file(file_path);
if (file.exists()) {
 mimageview.setimageuri(uri.fromfile(file));
}
  }
 }

 public void cameraapi(view view) {
  // 创建一个camera视图。
  // 将camera视图添加到cameralayout里。
  // 做一些camera的设置
  if (mcamera == null && mcameraview == null) {
mcamera = getcamera();
mcameraview = new cameraview(this, mcamera);
mcameralayout.addview(mcameraview);

camera.parameters parameters = mcamera.getparameters();
parameters.setrotation(90);
mcamera.setparameters(parameters);
  }
 }

 public void takephoto(view view) {
  // 点击按钮拍照,并保存到存储里。
  if (mcamera != null) {
try {
 mcamera.takepicture(null, null, new camera.picturecallback() {
  @override
  public void onpicturetaken(byte[] data, camera camera) {
file file = new file(file_path);
try {
 fileoutputstream fileoutputstream = new fileoutputstream(file);
 fileoutputstream.write(data);
 fileoutputstream.close();
} catch (filenotfoundexception e) {
 e.printstacktrace();
} catch (ioexception e) {
 e.printstacktrace();
}
  }
 });
} catch (exception e) {
 e.printstacktrace();
}
  }
 }

 public camera getcamera() {
  camera camera = null;
  try {
camera = camera.open();
  } catch (exception e) {
e.printstacktrace();
// 错误提示
  }
  return camera;
 }

 public class cameraview extends surfaceview implements surfaceholder.callback {

  camera mcamera;
  private final surfaceholder msurfaceholder;

  public cameraview(context context, camera camera) {
super(context);
mcamera = camera;
msurfaceholder = getholder();
msurfaceholder.addcallback(this);
  }

  @override
  public void surfacecreated(surfaceholder holder) {
try {
 if (mcamera != null) {
  mcamera.setpreviewdisplay(holder);
  mcamera.setdisplayorientation(90);
  mcamera.startpreview();
 }
} catch (ioexception e) {
 e.printstacktrace();
}
  }

  @override
  public void surfacechanged(surfaceholder holder, int format, int width, int height) {
if (msurfaceholder.getsurface() == null) {
 return;
}

if (mcamera != null) {
 mcamera.stoppreview();
}

try {
 if (mcamera != null) {
  mcamera.setpreviewdisplay(holder);
  mcamera.startpreview();
 }
} catch (ioexception e) {
 e.printstacktrace();
}

  }

  @override
  public void surfacedestroyed(surfaceholder holder) {
release();
  }

  public void release() {
if (mcamera != null) {
 mcamera.setpreviewcallback(null);
 mcamera.stoppreview();
 mcamera.release();
 mcamera = null;
}
  }
 }

 @override
 protected void onpause() {
  super.onpause();
  if (mcameraview != null) {
mcameraview.release();
  }
 }

 @override
 protected void onresume() {
  super.onresume();
  mcamera = getcamera();
  mcameralayout.removeallviews();
  mcameraview = new cameraview(this, mcamera);
  mcameralayout.addview(mcameraview);

  camera.parameters params = mcamera.getparameters();
  params.setrotation(90);
  mcamera.setparameters(params);
 }
}