gpt4 book ai didi

java - Android 相机的问题

转载 作者:行者123 更新时间:2023-11-30 04:48:02 24 4
gpt4 key购买 nike

我有一些我一直在试验的代码,看看我能用相机设备做什么。以下代码有效,但我有一些似乎无法解决的问题。

  1. 第一个电话永远不会成功。第一次运行代码时,永远不会调用 onPictureTaken 回调,因此永远不会写入文件。然而,相机会经历所有其他步骤,包括发出假的快门噪音。

  2. 我似乎无法将图片大小设置为默认值以外的值。如果我尝试将其设置为其他内容,代码将停止工作。与上面相同,相机完成所有 Action ,但永远不会调用 onPictureTaken 回调。

  3. 将照片保存到 DCIM 文件夹后,拍摄的照片不会显示在我手机上的默认“照片”应用中,除非我重新启动手机。

  4. 有没有办法通过代码来关闭快门噪音?

抱歉,代码有点乱,因为它是一个实验。

此外,此代码在 BroadcastReceiver 中执行

    @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub


if(intent.getAction().equals(TAKE_PICTURE_INTENT))
{


Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();


System.out.println("GOT THE INTENT");

try
{
Camera camera = Camera.open();

System.out.println("CAMERA OPENED");

Parameters params = camera.getParameters();
params.set("flash-mode", "off");
params.set("focus-mode", "infinity");
params.set("jpeg-quality", "100");
//params.setPictureSize(2592, 1952);

String str = params.get("picture-size" + "-values");
System.out.println(str);

String size = str.split(",")[0];
System.out.println(size);

//params.set("picture-size", size);

camera.setParameters(params);


System.out.println("CAMERA PARAMETERS SET");



camera.startPreview();
System.out.println("CAMERA PREVIEW STARTED");


camera.autoFocus(new AutoFocusCallBackImpl());


}
catch(Exception ex)
{
System.out.println("CAMERA FAIL, SKIP");
return ;
}


}//if

}//onreceive


private void TakePicture(Camera camera)
{
camera.takePicture(new Camera.ShutterCallback() {

@Override
public void onShutter() {
// TODO Auto-generated method stub

System.out.println("CAMERA SHUTTER CALLBACK");
}
}
, null,
new Camera.PictureCallback() {

public void onPictureTaken(byte[] imageData, Camera c) {
//c.release();

System.out.println("CAMERA CALLBACK");

FileOutputStream outStream = null;
try {

System.out.println("Start Callback");
File esd = Environment.getExternalStorageDirectory();

outStream = new FileOutputStream(esd.getAbsolutePath() + String.format(
"/DCIM/%d.jpg", System.currentTimeMillis()));

outStream.write(imageData);
outStream.close();
System.out.println( "onPictureTaken - wrote bytes: " + imageData.length);
} catch (FileNotFoundException e) {
e.printStackTrace();

System.out.println("File not found exception");
} catch (IOException e) {
e.printStackTrace();

System.out.println("IO exception");
} finally {
System.out.println("Finally");
c.release();
}

}
}
);
//camera.release();
}//TAKE PICTURE

private class AutoFocusCallBackImpl implements Camera.AutoFocusCallback {
@Override
public void onAutoFocus(boolean success, Camera camera) {
//bIsAutoFocused = success; //update the flag used in onKeyDown()
System.out.println("Inside autofocus callback. autofocused="+success);
//play the autofocus sound
//MediaPlayer.create(CameraActivity.this, R.raw.auto_focus).start();

if(success)
{
System.out.println("AUTO FOCUS SUCCEDED");
}
else
{
System.out.println("AUTO FOCUS FAILED");
}

TakePicture(camera);
System.out.println("CALLED TAKE PICTURE");

}
}//AUTOFOCUSCALLBACK

最佳答案

1.首先将所有摄像头逻辑从 BroadCast 接收器中取出并放入单独的 Activity 中。

2.

When the pictures are saved to the DCIM folder, the taken pictures do not show up in the default 'Photos' app on my phone, unless i reboot the phone.

因为拍照后需要调用 MediaScanner 重新扫描图像/变化。当您重新启动手机时,mediascanner 会扫描媒体并找到新图像。对于这个问题,您应该查看 MediaScanner。

3.关注安卓相机Tutorial & Camera API

-谢谢

关于java - Android 相机的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4342443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com