我想实现一项功能,当有人试图解锁我的设备并输入错误密码 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();
}
}
};
}
最佳答案
我已经测试了您的代码并得到了正确的图像,因此我认为您的相机代码工作正常。
你可以查看我的代码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 上工作。
关于android - 在设备被密码锁定时捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16075726/
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
我正在尝试使用ruby编写一个双线程客户端,一个线程从套接字读取数据并将其打印出来,另一个线程读取本地数据并将其发送到远程服务器。我发现的问题是Ruby似乎无法捕获线程内的错误,这是一个示例:#!/usr/bin/rubyThread.new{loop{$stdout.puts"hi"abc.putsefsleep1}}loop{sleep1}显然,如果我在线程外键入abc.putsef,代码将永远不会运行,因为Ruby将报告“undefinedvariableabc”。但是,如果它在一个线程内,则没有错误报告。我的问题是,如何让Ruby捕获这样的错误?或者至少,报告线程中的错误?
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e
有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/
Devise是一个Ruby库,它为我提供了这个User类:classUser当写入:confirmable时,注册时会发送一封确认邮件。上周我不得不批量创建300个用户,所以我在恢复之前注释掉了:confirmable几分钟。现在我正在为用户批量创建创建一个UI,因此我需要即时添加/删除:confirmable。(我也可以直接修改Devise的源码,但我宁愿不去调和它)问题:如何即时添加/删除:confirmable? 最佳答案 WayneConrad的解决方案:user=User.newuser.skip_confirmation
我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ