在表面视图中镜像图像:Android

时间:2013-12-12 12:15:29

标签: android android-camera

由于使用前置摄像头,所看到的视频是镜像的。我怎样才能正常。我用过这段代码。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


   Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
   Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);
   Button buttonCapturePreview = (Button) findViewById(R.id.capturepreview);


    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);


    rawCallback = new PictureCallback() 
    {
        public void onPictureTaken(byte[] data, Camera camera)
        {
            Log.d("Log", "onPictureTaken - raw");
        }
    };

    shutterCallback = new ShutterCallback()
    {
        public void onShutter() {
            Log.i("Log", "onShutter'd");
        }
    };

    jpegCallback = new PictureCallback()
    {
        public void onPictureTaken(byte[] data, Camera camera) 
         {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
            bitmap = flip(bitmap);
            Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Asw/Raw Image");
            imagesFolder.mkdirs(); 
            Date d = new Date();
            CharSequence s  = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
            name = s.toString() + ".jpg";

            File output = new File(imagesFolder, name);

            Uri uriSavedImage = Uri.fromFile(output);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            OutputStream imageFileOS;

            try {

                imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
                imageFileOS.write(data);
                imageFileOS.flush();
                imageFileOS.close();

                } 
            catch (FileNotFoundException e) 
                {
                e.printStackTrace();
                } 
            catch (IOException e)
            {
                e.printStackTrace();

            } 
            finally
            {}
            Log.d("Log", "onPictureTaken - jpeg");

         }

    buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
    {

        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void onClick(View v) 
        {
            if(!previewing)
            {
                camera = Camera.open(0);
                if (camera != null)
                {

                    try {

                        camera.setPreviewDisplay(surfaceHolder);
                        camera.startPreview();
                        previewing = true;

                        } 
                    catch (IOException e) 
                        {

                        e.printStackTrace();
                        }
                }else txtData.setText("null");
            }
        }});



    buttonCapturePreview.setOnClickListener(new OnClickListener()
   {

        @Override
        public void onClick(View v) 
        {

            captureImage();
        }


    });


    buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) 
        {

            if(camera != null && previewing)
            {
                camera.stopPreview();
                camera.release();
                camera = null;

                previewing = false;
            }
        }});



}

private void captureImage() 
{

    camera.takePicture(shutterCallback, rawCallback, jpegCallback);


}



BitmapDrawable flip(Bitamp bitmap)
{
    Matrix m = new Matrix();
    m.setScale(-1, 1);
    Bitmap bmp = d.getBitmap();
    Bitmap dst = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return new BitmapDrawable(dst);

}

请告诉我这个错误有什么问题。 flip()方法正在进行镜像处理......有什么问题吗?任何建议都会很明显

0 个答案:

没有答案