android前置拍照镜像代码,在Android中镜像前置摄像头

Stoycho Andr..

5

首先使用Camera.open()打开相机实例时,应使用Camera.open打开前置相机(getSpecialFacingCamera())

private int getSpecialFacingCamera() {

int cameraId = -1;

// Search for the front facing camera

int numberOfCameras = Camera.getNumberOfCameras();

for (int i = 0; i < numberOfCameras; i++) {

Camera.CameraInfo info = new Camera.CameraInfo();

Camera.getCameraInfo(i, info);

if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {

cameraId = i;

break;

}

}

return cameraId;

}

然后在您的回调方法中,相机数据转换为图像,您可以使用此代码保持正常

public void onPictureTaken(byte[] data, Camera camera){

Bitmap newImage = null;

Bitmap cameraBitmap;

if (data != null) {

cameraBitmap = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

// use matrix to reverse image data and keep it normal

Matrix mtx = new Matrix();

//this will prevent mirror effect

mtx.preScale(-1.0f, 1.0f);

// Setting post rotate to 90 because image will be possibly in landscape

mtx.postRotate(90.f);

// Rotating Bitmap , create real image that we want

newImage = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), mtx, true);

}else{// LANDSCAPE MODE

//No need to reverse width and height

newImage = Bitmap.createScaledBitmap(cameraBitmap, screenWidth, screenHeight, true);

cameraBitmap = newImage;

}

}

}

你可以在画布中传递newImage并创建jpeg图像并将其保存在设备上.别忘了相机在Api等级21中已被弃用...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值