• 45_拍照


    public class MainActivity extends Activity {
        private View layout;
        private Camera camera;
        
        @Override
        public 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.main);
            
            layout = this.findViewById(R.id.buttonlayout);
            
            SurfaceView surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
            surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            surfaceView.getHolder().setFixedSize(176, 144);
            surfaceView.getHolder().setKeepScreenOn(true);
            surfaceView.getHolder().addCallback(new SurfaceCallback());
        }
    
        public void takepicture(View v){
            if(camera!=null){
                switch (v.getId()) {
                case R.id.takepicture:
                    camera.takePicture(null, null, new MyPictureCallback());
                    break;
    
                case R.id.autofocus:
                    camera.autoFocus(null);
                    break;
                }
            }
        }
     
        private final class MyPictureCallback implements PictureCallback{
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                    File jpgFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");
                    FileOutputStream outStream = new FileOutputStream(jpgFile);
                    outStream.write(data);
                    outStream.close();
    camera.startPreview();
    !! } catch (Exception e) { e.printStackTrace(); } } } private final class SurfaceCallback implements Callback{ public void surfaceCreated(SurfaceHolder holder) { try{ camera = Camera.open();//打开摄像头 Camera.Parameters parameters = camera.getParameters(); //Log.i("MainActivity", parameters.flatten()); parameters.setPreviewSize(800, 480); parameters.setPreviewFrameRate(5); parameters.setPictureSize(1024,768); parameters.setJpegQuality(80); camera.setParameters(parameters); camera.setPreviewDisplay(holder); camera.startPreview();//开始预览 }catch (Exception e) { e.printStackTrace(); } } public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { } public void surfaceDestroyed(SurfaceHolder holder) { // 及时释放 camera if(camera!=null){ camera.release(); camera = null; } } } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ layout.setVisibility(ViewGroup.VISIBLE); return true; } return super.onTouchEvent(event); } }
  • 相关阅读:
    什么叫线程安全?servlet 是线程安全吗?
    SynchronizedMap 和 ConcurrentHashMap 有什么区别?
    CopyOnWriteArrayList 可以用于什么应用场景?
    乐观锁和悲观锁的理解及如何实现,有哪些实现方式?
    当一个线程进入某个对象的一个 synchronized 的实例方 法后,其它线程是否可进入此对象的其它方法?
    一个线程运行时发生异常会怎样?
    用 Java 实现阻塞队列 ?
    在 java 中 wait 和 sleep 方法的不同?
    为什么代码会重排序?
    volatile 有什么用?能否用一句话说明下 volatile 的应用场景?
  • 原文地址:https://www.cnblogs.com/carl2380/p/4168208.html
Copyright © 2020-2023  润新知