使用密码锁定设备时捕获图像

时间:2013-04-18 06:12:46

标签: android android-intent android-camera android-canvas android-camera-intent

当有人试图解锁我的设备并输入错误的密码3次时,我想实现一种通过前置摄像头捕获图像的功能。我检查过它可以在Android中使用,某些应用程序也可以在Market中使用。

我已经做了一些工作来实现这个目标但是我得到了一个黑色的图像。这是代码:

向设备管理员注册,以获取广播的错误密码尝试:

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);

}

}

摄像机类捕获图像并将其保存到SD卡:

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();
        }
    }
};
}

2 个答案:

答案 0 :(得分:6)

我已经测试了你的代码,我得到了一个合适的图像,所以我认为你的相机代码工作正常。

您可以查看我的代码here。 我复制了你的CameraView类。 我从https://github.com/marakana/DevicePolicyDemo获取的设备管理部分 第一次尝试失败时拍摄照片。

我怀疑它可能是某种硬件问题。 我不确定你是否见过Taking picture from camera without preview

第二个答案提到假冒SurfaceView伪造预览并不适用于所有Android设备。请看他的blog。他在各种手机上都做过测试。 您可以尝试我的项目,如果它不适用于您的手机。情况可能就是这样。 (其他答案也可能对您有用。)

我用CM10在Galaxy S3上尝试了我的代码。

希望这有帮助。

Edit1:在HTC One,HTC One X,Sony Experia上测试过。工作正常。

Edit2:使用Galaxy Note。

答案 1 :(得分:0)

我不确定这会在其他设备上运行,但如果确实如此,它可能会让我们深入了解最新情况。

在Camera.Parameters中,有一项功能可将原始托架图像直接保存到SD卡。此图像在创建.jpeg文件之前发生,因此如果操作系统消隐图像,则可能不会影响此图像。

  Camera.Parameters parameters=camera.getParameters();
  parameters.set("rawsave-mode", "1");
  parameters.set("rawfname", "/mnt/sdcard2/test5.raw");
  camera.setParameters(parameters);

如果此功能适用于您的设备,您应该能够确定是拍摄和删除照片,还是根本不拍照。

如果原始图像有效,将Bayer转换为RGB图像或灰度图像相当容易。

我要查看的另一个区域是logcat信息,并比较代码的未锁定上下文和代码的锁定上下文之间的日志。如果存在安全问题,则应该反映在logcat中。

相关问题