public void onPreviewFrame(byte[]data,Camera arg 1)からBitmap(収集)を入手

3925 ワード

        private PreviewCallback pc = new PreviewCallback(){
            public void onPreviewFrame(byte[] data, Camera arg1) {
                Camera.Parameters parameters=arg1.getParameters(); 
                int imageFormat=parameters.getPreviewFormat();  
                int w=parameters.getPreviewSize().width;  
                int h=parameters.getPreviewSize().height;
                Rect rect=new Rect(0,0,w,h);
                YuvImage yuvImg = new YuvImage(data,imageFormat,w,h,null);
                try {  
                    ByteArrayOutputStream outputstream = new ByteArrayOutputStream(); 
                    yuvImg.compressToJpeg(rect, 100, outputstream);  
                    appBitmap.rawbitmap = BitmapFactory.decodeByteArray(outputstream.toByteArray(), 0, outputstream.size());  



                    //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/fp.jpg"));
                    //img.compressToJpeg(rect, 100, bos);
                    //bos.flush();  
                    //bos.close();  
                    camera.startPreview();
                    } 
                catch (Exception e) {  }
                
                if(5 == random.nextInt(8)){
                    Message msgCapture = Message.obtain();
                    msgCapture.what = success;
                    msgHandler.sendMessage(msgCapture);
                    }
                
                else{
                    Message msgCapture = Message.obtain();
                    msgCapture.what = failure;
                    msgHandler.sendMessage(msgCapture);
                    }
                }
        };

ソース:http://blog.csdn.net/hipilee/article/details/8629234