android – 旋转手机快速180度,相机预览颠倒

android – 旋转手机快速180度,相机预览颠倒,第1张

概述我有自定义相机应用程序.当我将手机旋转90度时,相机预览工作正常.但是当我将手机快速旋转180度时,相机预览会颠倒过来.有没有解决方案…… 这是我的代码如下: public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback {Camera camera;SurfaceView s 我有自定义相机应用程序.当我将手机旋转90度时,相机预览工作正常.但是当我将手机快速旋转180度时,相机预览会颠倒过来.有没有解决方案……

这是我的代码如下:

public class CustomCameraActivity extends Activity implements    SurfaceHolder.Callback {Camera camera;SurfaceVIEw surfaceVIEw;SurfaceHolder surfaceHolder;boolean prevIEwing = false;LayoutInflater controlinflater = null;private Sensor mOrIEntaion1;int cameraID = 0;public final String TAG = "CustomCamera";private SensorManager sensorManager;@OverrIDepublic 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);    context = this;    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);    mOrIEntaion1 = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);    // setRequestedOrIEntation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imgError);    getwindow().setFormat(PixelFormat.UNKNowN);    surfaceVIEw = (SurfaceVIEw) findVIEwByID(R.ID.cameraprevIEw);    surfaceHolder = surfaceVIEw.getHolder();    surfaceHolder.addCallback(this);    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    controlinflater = LayoutInflater.from(getBaseContext());    VIEw vIEwControl = controlinflater.inflate(R.layout.custom,null);    LayoutParams layoutParamsControl = new LayoutParams(            LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);    button btn1 = (button) vIEwControl.findVIEwByID(R.ID.button01);    button btn2 = (button) vIEwControl.findVIEwByID(R.ID.button02);    btn1.setonClickListener(new OnClickListener() {        @OverrIDe        public voID onClick(VIEw arg0) {            // Toast.makeText(context,"1111111111111111111111111",// Toast.LENGTH_SHORT).show();            camera.takePicture(null,null,mPicture);            Constant.rotationValueForCamera = Constant.rotationValue;        }    });    btn2.setonClickListener(new OnClickListener() {        @OverrIDe        public voID onClick(VIEw arg0) {            // Toast.makeText(context,"22222222222222222222222222",// Toast.LENGTH_SHORT).show();            Log.e("0 imagePickerStatus",Constant.imagePickerStatus + "");            Constant.imagePickerStatus = 0;            Log.e("0 imagePickerStatus",Constant.imagePickerStatus + "");            finish();        }    });    this.addContentVIEw(vIEwControl,layoutParamsControl);    int ot = getResources().getConfiguration().orIEntation;    if (Configuration.ORIENTATION_LANDSCAPE == ot) {        imageVIEw.setVisibility(VIEw.GONE);        Log.e("ori1111","land");    } else {        imageVIEw.setVisibility(VIEw.VISIBLE);        Log.e("ori111","port");    }}@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) {    super.onConfigurationChanged(newConfig);    // Checks the orIEntation of the screen    if (newConfig.orIEntation == Configuration.ORIENTATION_LANDSCAPE) {        // Toast.makeText(this,"landscape",Toast.LENGTH_SHORT).show();        findVIEwByID(R.ID.button01).setVisibility(VIEw.VISIBLE);        findVIEwByID(R.ID.button02).setVisibility(VIEw.VISIBLE);        imageVIEw.setVisibility(VIEw.GONE);        Log.e("ori","land");    } else if (newConfig.orIEntation == Configuration.ORIENTATION_PORTRAIT) {        // Toast.makeText(this,"portrait",Toast.LENGTH_SHORT).show();        findVIEwByID(R.ID.button01).setVisibility(VIEw.INVISIBLE);        findVIEwByID(R.ID.button02).setVisibility(VIEw.INVISIBLE);        imageVIEw.setVisibility(VIEw.VISIBLE);        Log.e("ori","port");    }}public String getPollDeviceAttitude() {    return Constant.rotationValueForCamera;}private SensorEventListener sensorEventListener = new SensorEventListener() {    public voID onAccuracyChanged(Sensor sensor,int accuracy) {    }};protected voID onPause() {    super.onPause();    sensorManager.unregisterListener(sensorEventListener);}@OverrIDepublic voID onResume() {    super.onResume();    sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_norMAL);    sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_norMAL);    if (Constant.isCapturedOk) {        Constant.isCapturedOk = false;        finish();    }}PictureCallback mPicture = new PictureCallback() {    @OverrIDe    public voID onPictureTaken(byte[] data,Camera camera) {        Log.e("Camrera","22222222222222222");        BitmapFactory.Options bfo = new BitmapFactory.Options();        bfo.inDither = false;        // bfo.inJustDecodeBounds = true;        bfo.inPurgeable = true;        bfo.inTempStorage = new byte[16 * 1024];        Intent intent = new Intent(context,PrevIEwActivity.class);        // intent.putExtra("data",data);        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data,data.length,bfo);        Matrix matrix = new Matrix();        if (Constant.result == 180) {            matrix.postRotate(270);        }        if (Constant.result == 270) {            matrix.postRotate(180);        }        int height = bitmapPicture.getHeight();        int wIDth = bitmapPicture.getWIDth();        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapPicture,height,wIDth,true);        Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap,scaledBitmap.getWIDth(),scaledBitmap.getHeight(),matrix,true);        ByteArrayOutputStream blob = new ByteArrayOutputStream();        Log.e("Camrera1","22222222222222222");        rotatedBitmap.compress(CompressFormat.JPEG,50 /* ignored for PNG */,blob);        byte[] bitmapdata = blob.toByteArray();        Constant.imageData = bitmapdata;        Log.e("Camrera2","22222222222222222");        startActivity(intent);    }};@OverrIDepublic voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) {    if (prevIEwing) {        camera.stopPrevIEw();        prevIEwing = false;    }    if (camera != null) {        try {            camera.setPrevIEwdisplay(holder);            camera.startPrevIEw();            setCameradisplayOrIEntation(this,cameraID,camera);            prevIEwing = true;        } catch (Exception e) {            e.printstacktrace();        }    }}public static voID setCameradisplayOrIEntation(Activity activity,int cameraID,androID.harDWare.Camera camera) {    androID.harDWare.Camera.CameraInfo info = new androID.harDWare.Camera.CameraInfo();    androID.harDWare.Camera.getCameraInfo(cameraID,info);    int rotation = activity.getwindowManager().getDefaultdisplay()            .getRotation();    int degrees = 0;    switch (rotation) {    case Surface.ROTATION_0:        degrees = 0;        Constant.result = 0;        break;    case Surface.ROTATION_90:        degrees = 90;        Constant.result = 90;        break;    case Surface.ROTATION_180:        degrees = 180;        Constant.result = 180;        break;    case Surface.ROTATION_270:        degrees = 270;        Constant.result = 270;        break;    }    int result;    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {        result = (info.orIEntation + degrees) % 360;        result = (360 - result) % 360; // compensate the mirror    } else { // back-facing        result = (info.orIEntation - degrees + 360) % 360;    }    camera.setdisplayOrIEntation(result);}@OverrIDepublic voID surfaceCreated(SurfaceHolder holder) {    camera = Camera.open();}@OverrIDepublic voID surfaceDestroyed(SurfaceHolder holder) {    camera.stopPrevIEw();    camera.release();    camera = null;    prevIEwing = false;}@OverrIDeprotected voID onStop() {    super.onStop();    Log.e("Tab","StoPing");}@OverrIDepublic boolean onKeyDown(int keyCode,KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_BACK) {        return true;    }    return super.onKeyDown(keyCode,event);}

}

解决方法 您可以使用 OrientationEventListener触发重新计算相机旋转.

添加到您的活动:

private OrIEntationEventListener orIEntationListener = null;

到onCreate():

orIEntationListener = new OrIEntationEventListener(this) {    public voID onorIEntationChanged(int orIEntation) {        setCameradisplayOrIEntation(CustomCameraActivity.this,camera);    }};

到surfaceCreated():

orIEntationListener.enable();

到surfaceDestroyed():

orIEntationListener.disable();

现在,它几乎可以工作.要使setCameradisplayOrIEntation()更健壮,

>添加相机检查!= null>如果自上次调用函数后结果发生更改,则仅调用camera.setdisplayOrIEntation(result)(或执行任何繁重的 *** 作).

总结

以上是内存溢出为你收集整理的android – 旋转手机快速180度,相机预览颠倒全部内容,希望文章能够帮你解决android – 旋转手机快速180度,相机预览颠倒所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1137492.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存