11

Tôi muốn thực hiện chức năng chụp ảnh qua camera trước khi ai đó cố gắng mở khóa thiết bị của tôi và nhập mật khẩu không chính xác 3 lần. Tôi đã kiểm tra xem có thể có trong Android và một số ứng dụng có sẵn trong Market hay không.Chụp ảnh trong khi thiết bị bị khóa bằng mật khẩu

Tôi đã thực hiện một số công việc để đạt được điều này nhưng tôi nhận được một hình ảnh đen. Dưới đây là các mã:

ký với thiết bị quản trị để có được phát sóng cho nỗ lực mật khẩu không chính xác:

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

} 

} 

Máy ảnh Class để chụp ảnh và lưu nó vào thẻ 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(); 
     } 
    } 
}; 
} 
+0

Bạn đã thử nghiệm mã khi điện thoại được mở khóa chưa? –

+0

Có Tôi có thể chụp ảnh bằng mã này trong khi điện thoại được mở khóa ... –

+0

@KrishnakantDalal: Bạn đã kiểm tra nhật ký bảng điều khiển Android chưa. Có thông báo lỗi nào không? –

Trả lời

6

Tôi đã thử nghiệm mã của bạn và tôi có được hình ảnh phù hợp để tôi cho rằng mã máy ảnh của bạn hoạt động tốt.

Bạn có thể kiểm tra mã của mình here. Tôi đã sao chép lớp CameraView từ máy của bạn. Phần quản trị thiết bị tôi lấy từ https://github.com/marakana/DevicePolicyDemo Hình ảnh được chụp khi lần thử đầu tiên không thành công.

Tôi nghi ngờ rằng đó có thể là một số loại vấn đề phần cứng. Tôi không chắc chắn nếu bạn đã nhìn thấy Taking picture from camera without preview.

Câu trả lời thứ hai có đề cập rằng giả mạo bản xem trước với SurfaceView giả không hoạt động trên tất cả các thiết bị Android. Vui lòng xem số blog của anh ấy. Anh ấy đã làm bài kiểm tra trên nhiều điện thoại khác nhau. Bạn có thể thử dự án của tôi và nếu nó không hoạt động trên điện thoại của bạn. Đây có thể là trường hợp. (Các câu trả lời khác cũng có thể hữu ích cho bạn.)

Tôi đã thử mã trên Galaxy S3 với CM10.

Hy vọng điều này sẽ hữu ích.

Chỉnh sửa1: Đã thử nghiệm trên HTC One, HTC One X, Sony Experia. Hoạt động tốt.

Chỉnh sửa2: Làm việc trên Galaxy Note.

+0

Cảm ơn rất nhiều ... Nó thực sự hữu ích. Lần đầu tiên tôi thử nghiệm trên Samsung Nexus nhưng sau đó tôi thử nghiệm điều này trên số thiết bị khác và nó đang hoạt động trên tất cả ngoại trừ một Nexus của Samsung. Tôi thử nghiệm ứng dụng trên LG Nexus, Samsung Galaxy Tab 7 'và 10', Samsung SII và Samsung SII. Cảm ơn bạn rất nhiều lần nữa .... –

+0

bạn được chào đón – pt2121

+0

Xin hãy ủng hộ tôi, Nếu bạn kiểm tra mã này trong thiết bị khác, vui lòng tiếp tục cập nhật tại đây để chắc chắn rằng ứng dụng của tôi đang hỗ trợ số lượng thiết bị. –

0

Tôi không chắc chắn điều này sẽ làm việc trên các thiết bị khác, nhưng nếu nó có thể cung cấp một số cái nhìn sâu sắc vào những gì đang xảy ra.

Trong Camera.Parameters, có một tính năng để lưu hình ảnh bộ đệm bay trực tiếp vào thẻ SD. Hình ảnh này xảy ra trước khi tệp .jpeg được tạo, do đó, nếu hệ điều hành làm trống hình ảnh, hình ảnh này có thể không bị ảnh hưởng.

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

Nếu tính năng này hoạt động trên thiết bị của bạn, bạn sẽ có thể để xác định xem các hình ảnh đã được thực hiện và tiêu diệt hoặc nếu nó không được thực hiện tại tất cả.

Nếu hình ảnh thô hoạt động, việc chuyển đổi trình phát sang hình ảnh RGB hoặc thang độ xám là khá dễ dàng.

Khu vực khác tôi muốn tìm là thông tin nhật ký và so sánh nhật ký giữa ngữ cảnh mở khóa của mã và ngữ cảnh bị khóa của mã. Nếu có vấn đề về bảo mật, nó sẽ được phản ánh trong logcat.

Các vấn đề liên quan