捕获图像,而设备锁定与密码 [英] Capture Image while Device is Locked with Password

查看:96
本文介绍了捕获图像,而设备锁定与密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个功能,通过前置摄像头捕捉到的图像,当有人试图解开我的设备,然后输入不正确的密码3次。我查了一下,有可能在Android和一些应用程序也可在市场。

我已经做了一些工作,实现这一点,但我得到一个黑色的图像。这里的code:

与设备管理注册以获得广播不正确的密码尝试:

 公共类DeviceAdminSample扩展DeviceAdminReceiver {

静态上下文CTX;

静态共享preferences的getSample preferences(上下文的背景下){

    CTX =背景;

    返回context.getShared preferences(
            DeviceAdminReceiver.class.getName(),0);
}

@覆盖
公共无效onPasswordFailed(上下文的背景下,意图意图){
    super.onPasswordFailed(背景下,意图);

    的System.out.println(密码尝试失败......);

    意图I =新的意图(背景下,CameraView.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(ⅰ);

}

}
 

Camera类捕获图像,并将其保存到SD卡:

 公共类CameraView扩展活动实现SurfaceHolder.Callback,
    OnClickListener {
私有静态最后字符串变量=CameraTest;
相机mCamera;
布尔米previewRunning = FALSE;

公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    Log.e(TAG的onCreate);

    的setContentView(R.layout.cameraview);

    mSurfaceView =(SurfaceView)findViewById(R.id.surface_camera);

    // mSurfaceView.setOnClickListener(本);

    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(本);

    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mSurfaceHolder.setKeepScreenOn(真正的);

    // mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@覆盖
保护无效onRestoreInstanceState(包savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
}

保护无效onResume(){
    Log.e(TAG,onResume);
    super.onResume();
}

保护无效的onSaveInstanceState(包outState){
    super.onSaveInstanceState(outState);
}

保护无效的onStop(){
    Log.e(TAG的onStop);
    super.onStop();
}

公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){
    Log.e(TAG,surfaceChanged);

    // XXX停止preVIEW()会崩溃,如果preVIEW没有运行
    如果(M previewRunning){
        mCamera.stop preVIEW();
    }

    Camera.Parameters p值= mCamera.getParameters();

    mCamera.setParameters(对);

    mCamera.start preVIEW();
    米previewRunning = TRUE;
    mCamera.takePicture(NULL,NULL,mPictureCallback);


}

公共无效surfaceDestroyed(SurfaceHolder持有者){
    Log.e(TAG,surfaceDestroyed);
    // mCamera.stop preVIEW();
    //米previewRunning = FALSE;
    // mCamera.release();

    stopCamera();
}

私人SurfaceView mSurfaceView;
私人SurfaceHolder mSurfaceHolder;

公共无效的onClick(视图v){
    mCamera.takePicture(NULL,mPictureCallback,mPictureCallback);
}

公共无效surfaceCreated(SurfaceHolder持有者){
    Log.e(TAG,surfaceCreated);

    INT I = findFrontFacingCamera();

    如果(ⅰ大于0);
    而(真){
        尝试 {
            this.mCamera = Camera.open(ⅰ);
            尝试 {
                this.mCamera.set previewDisplay(保持器);
                返回;
            }赶上(IOException异常localIOException2){
                stopCamera();
                返回;
            }
        }赶上(RuntimeException的localRuntimeException){
            localRuntimeException.printStackTrace();
            如果(this.mCamera == NULL)
                继续;
            stopCamera();
            this.mCamera = Camera.open(ⅰ);
            尝试 {
                this.mCamera.set previewDisplay(保持器);
                Log.d(HiddenEye加,相机开RE);
                返回;
            }赶上(IOException异常localIOException1){
                stopCamera();
                localIOException1.printStackTrace();
                返回;
            }

        }赶上(例外localException){
            如果(this.mCamera!= NULL)
                stopCamera();
            localException.printStackTrace();
            返回;
        }
    }
}

私人无效stopCamera(){
    如果(this.mCamera!= NULL){
        /*this.mCamera.stop$p$pview();
        this.mCamera.release();
        this.mCamera = NULL; * /
        this.m previewRunning = FALSE;
    }
}

私人诠释findFrontFacingCamera(){
    INT I = Camera.getNumberOfCameras();
    对于(INT J = 0 ;; J ++){
        如果(J> = I)
            返回-1;
        Camera.CameraInfo localCameraInfo =新Camera.CameraInfo();
        Camera.getCameraInfo(J,localCameraInfo);
        如果(localCameraInfo.facing == 1)
            复位J;
    }
}

Camera.PictureCallback mPictureCallback =新Camera.PictureCallback(){

    公共无效onPictureTaken(byte []的数据,摄像头摄像头){
        如果(数据!= NULL){
            //意图mIntent =新意图();
            // mIntent.putExtra(形象,为imageData);

            mCamera.stop preVIEW();
            米previewRunning = FALSE;
            mCamera.release();

            尝试 {
                BitmapFactory.Options选择采用=新BitmapFactory.Options();
                点阵位图= BitmapFactory.de codeByteArray(数据,0,
                        data.length,选择采用);
                位= Bitmap.createScaledBitmap(位图,300,300,FALSE);
                INT宽度= bitmap.getWidth();
                INT高= bitmap.getHeight();
                INT newWidth = 300;
                INT newHeight = 300;

                //计算比例 - 在这种情况下= 0.4f
                浮动scaleWidth =((浮点)newWidth)/宽度;
                浮动scaleHeight =((浮点)newHeight)/身高;

                // createa矩阵用于操作
                字模=新的Matrix();
                //调整位图
                matrix.postScale(scaleWidth,scaleHeight);
                //旋转位图
                matrix.postRotate(-90);
                位图resizedBitmap = Bitmap.createBitmap(位图,0,0,
                        宽度,高度,矩阵,真);

                ByteArrayOutputStream字节=新ByteArrayOutputStream();
                resizedBitmap.com preSS(Bitmap.Com pressFormat.JPEG,40,
                        字节);

                //您可以创建在SD卡新文件名test.jpg放在
                // 夹。
                文件F =新的文件(Environment.getExternalStorageDirectory()
                        文件分割符+ +test.jpg放在);

                的System.out.println(文件F:+ F);

                f.createNewFile();
                //写在文件中的字节
                FileOutputStream中FO =新的FileOutputStream(F);
                fo.write(bytes.toByteArray());

                //记得接近德FileOutput
                fo.close();

            }赶上(例外五){
                e.printStackTrace();
            }
            // StoreByteImage(mContext,为imageData,50,ImageName);
            //的setResult(FOTO_MODE,mIntent);
            的setResult(585);
            完();
        }
    }
};
}
 

解决方案

我已经测试你的code和我得到一个正确的图像,所以我觉得你的相机code正常工作。

您可以检查我的code 这里。 我从你复制的CameraView类。 设备管理部分,我从 https://github.com/marakana/DevicePolicyDemo 了 当第一次尝试失败了拍照。

我怀疑它可能是某种类型的硬件问题。 我不知道如果你看过 Android摄像头没有preVIEW

第二个答案也提到,伪造了preVIEW与虚拟SurfaceView不能在所有的Andr​​oid设备正常工作。请看到他的博客。他做了各种手机的测试。 你可以尝试我的项目,如果没有您的手机上运行。这可能是这种情况。 (其他的答案也可能对您有用。)

我想我的code对银河S3与CM10。

希望这有助于。

EDIT1:在测试的HTC One,HTC One X的,索尼Experia还。正常工作。

EDIT2:曾在Galaxy Note的

I want to implement a functionality to capture image through front camera when someone tries to unlock my device and enter incorrect password 3 times. I checked that it is possible in Android and some applications are also available in Market.

I have done some work to achieve this but I am getting a black Image. Here's the code :

Register with Device admin to get the broadcast for incorrect password attempt :

public class DeviceAdminSample extends DeviceAdminReceiver {

static Context ctx;

static SharedPreferences getSamplePreferences(Context context) {

    ctx = context;

    return context.getSharedPreferences(
            DeviceAdminReceiver.class.getName(), 0);
}

@Override
public void onPasswordFailed(Context context, Intent intent) {
    super.onPasswordFailed(context, intent);

    System.out.println("Password Attempt is Failed...");

    Intent i = new Intent(context, CameraView.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

}

}

Camera Class to capture image and save it to SD Card :

public class CameraView extends Activity implements SurfaceHolder.Callback,
    OnClickListener {
private static final String TAG = "CameraTest";
Camera mCamera;
boolean mPreviewRunning = false;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.e(TAG, "onCreate");

    setContentView(R.layout.cameraview);

    mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);

    // mSurfaceView.setOnClickListener(this);

    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);

    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mSurfaceHolder.setKeepScreenOn(true);

    // mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

protected void onResume() {
    Log.e(TAG, "onResume");
    super.onResume();
}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

protected void onStop() {
    Log.e(TAG, "onStop");
    super.onStop();
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.e(TAG, "surfaceChanged");

    // XXX stopPreview() will crash if preview is not running
    if (mPreviewRunning) {
        mCamera.stopPreview();
    }

    Camera.Parameters p = mCamera.getParameters();

    mCamera.setParameters(p);

    mCamera.startPreview();
    mPreviewRunning = true;
    mCamera.takePicture(null, null, mPictureCallback);


}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e(TAG, "surfaceDestroyed");
    // mCamera.stopPreview();
    // mPreviewRunning = false;
    // mCamera.release();

    stopCamera();
}

private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;

public void onClick(View v) {
    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.e(TAG, "surfaceCreated");

    int i = findFrontFacingCamera();

    if (i > 0); 
    while (true) {
        try {
            this.mCamera = Camera.open(i);
            try {
                this.mCamera.setPreviewDisplay(holder);
                return;
            } catch (IOException localIOException2) {
                stopCamera();
                return;
            }
        } catch (RuntimeException localRuntimeException) {
            localRuntimeException.printStackTrace();
            if (this.mCamera == null)
                continue;
            stopCamera();
            this.mCamera = Camera.open(i);
            try {
                this.mCamera.setPreviewDisplay(holder);
                Log.d("HiddenEye Plus", "Camera open RE");
                return;
            } catch (IOException localIOException1) {
                stopCamera();
                localIOException1.printStackTrace();
                return;
            }

        } catch (Exception localException) {
            if (this.mCamera != null)
                stopCamera();
            localException.printStackTrace();
            return;
        }
    }
}

private void stopCamera() {
    if (this.mCamera != null) {
        /*this.mCamera.stopPreview();
        this.mCamera.release();
        this.mCamera = null;*/
        this.mPreviewRunning = false;
    }
}

private int findFrontFacingCamera() {
    int i = Camera.getNumberOfCameras();
    for (int j = 0;; j++) {
        if (j >= i)
            return -1;
        Camera.CameraInfo localCameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(j, localCameraInfo);
        if (localCameraInfo.facing == 1)
            return j;
    }
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {
        if (data != null) {
            // Intent mIntent = new Intent();
            // mIntent.putExtra("image",imageData);

            mCamera.stopPreview();
            mPreviewRunning = false;
            mCamera.release();

            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
                        data.length, opts);
                bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int newWidth = 300;
                int newHeight = 300;

                // calculate the scale - in this case = 0.4f
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;

                // createa matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);
                // rotate the Bitmap
                matrix.postRotate(-90);
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        width, height, matrix, true);

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 40,
                        bytes);

                // you can create a new file name "test.jpg" in sdcard
                // folder.
                File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.jpg");

                System.out.println("File F : " + f );

                f.createNewFile();
                // write the bytes in file
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                // remember close de FileOutput
                fo.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            // StoreByteImage(mContext, imageData, 50,"ImageName");
            // setResult(FOTO_MODE, mIntent);
            setResult(585);
            finish();
        }
    }
};
}

解决方案

I have tested your code and I get a proper image so I think your camera code works fine.

You can check my code here. I copied the CameraView class from yours. The device admin part I took from https://github.com/marakana/DevicePolicyDemo The picture is taken when the first attempt fails.

I doubt that it might be some kind of hardware issue. I am not sure if you've seen Android Camera without Preview.

The second answer there mentions that faking the preview with a dummy SurfaceView does not work on all Android devices. Please see his blog. He's done the test on various phones. You could try my project and if it doesn't work on your phone. This might be the case. (The other answers might also be useful for you.)

I tried my code on Galaxy S3 with CM10.

Hope this helps.

Edit1: Tested on HTC One, HTC One X, Sony Experia. Works fine.

Edit2: Worked on Galaxy Note.

这篇关于捕获图像,而设备锁定与密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆