位置: 编程技术 - 正文
推荐整理分享android照相机简单实例(android拍照),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:安卓照相机,android相机app,android相机app,android拍照,android 相机,android相机拍照,android照相功能的实现,android 相机,内容如对您有帮助,希望把文章链接给更多的朋友!
package com.example.mycamera;
import java.io.File;import java.io.FileOutputStream;import android.app.Activity;import android.content.pm.ActivityInfo;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.hardware.Camera;import android.hardware.Camera.PictureCallback;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {private Button button;private SurfaceView surfaceView;private SurfaceHolder holder;private Camera camera;private ImageView imageView;private Bitmap bitmap;private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {//更新左下角上次照片预览小窗口if (msg.what == 1) {LayoutParams params = imageView.getLayoutParams();params.height = ;params.width = ;imageView.setLayoutParams(params);imageView.setImageBitmap(bitmap);}}};@Overrideprotected void onResume() {if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}super.onResume();}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏setContentView(R.layout.activity_main);button = (Button) this.findViewById(R.id.nagetivebutton);button.setOnClickListener(new Mybuttonclicklistener());imageView = (ImageView) this.findViewById(R.id.imageView1);surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);holder = surfaceView.getHolder();holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);holder.setFixedSize(, );holder.setKeepScreenOn(true);holder.addCallback(new Myholdercallback());//添加回调响应}private class Myholdercallback implements Callback {@Overridepublic void surfaceCreated(SurfaceHolder holder) {try {camera = Camera.open();Camera.Parameters parameters = camera.getParameters();// parameters.setPreviewSize(, );// parameters.setPreviewFrameRate(5);// parameters.setPictureSize(, );parameters.setJpegQuality();camera.setParameters(parameters);camera.setPreviewDisplay(holder);//相机预览显示在surfaceview控件上camera.startPreview();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (camera != null) {camera.release();camera = null;}}}private final class PictureCallbackofme implements PictureCallback {@Overridepublic void onPictureTaken(byte[] data, Camera camera) {bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);Message message = Message.obtain();message.what = 1;handler.sendMessage(message);File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() ".jpg");try {FileOutputStream outputStream = new FileOutputStream(file);outputStream.write(data);outputStream.close();camera.startPreview();} catch (Exception e) {e.printStackTrace();}}}private class Mybuttonclicklistener implements OnClickListener {@Overridepublic void onClick(View v) {camera.takePicture(null, null, new PictureCallbackofme());}}//点击屏幕自动聚焦@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {camera.autoFocus(null);}return super.onTouchEvent(event);}}EditText隐藏软键盘的方法 法一:在Manifest.xml文件中的Activity标签中加入android:windowSoftInputMode=adjustPan|stateHiddenadjustPan:目的是为了避免软键盘弹出时,将控件往上顶,导致界面变形,st
ListView Item点击事件跳转详情界面 有好久都没有碰安卓了,考试加五一双组合也真是让人蛋疼。好在都平安的度过了,所以又有时间gun回来弄弄我的小博客(虽然没多少人看==)。看了之
android Fragment 入门介绍 FragmentFragment产生,优点,用途,使用方法简介1Fragmeng简介自从Android3.0中引入fragments的概念,根据词海的翻译可以译为:碎片、片段。其上的是为了解决不
标签: android拍照
本文链接地址:https://www.jiuchutong.com/biancheng/374603.html 转载请保留说明!友情链接: 武汉网站建设