gpt4 book ai didi

image - 在相机捕获图像 Android 上设置实时帧(错误)

转载 作者:行者123 更新时间:2023-12-05 07:38:30 26 4
gpt4 key购买 nike

我在执行下面这段代码时遇到错误。我在 CameraActivity.java 上出错(请看我的图片附件)谁能解决这个问题?

这是CameraActivity.java

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback{

private Camera camera = null;
private SurfaceView cameraSurfaceView = null;
private SurfaceHolder cameraSurfaceHolder = null;
private boolean previewing = false;

private Display display = null;


private static int wid = 0, hgt = 0;

private LayoutInflater layoutInflater = null;
private View cameraViewControl = null;
private ActionBar.LayoutParams layoutParamsControl = null;

private Button btnCapture = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

display = getWindowManager().getDefaultDisplay();
wid = display.getWidth();
hgt = display.getHeight();

getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.cameraoverlay);

cameraSurfaceView = (SurfaceView)findViewById(R.id.cameraSurfaceView);
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

layoutInflater = LayoutInflater.from(getBaseContext());
layoutParamsControl = new ActionBar.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT);

cameraViewControl = layoutInflater.inflate(R.layout.cambutton,null);
this.addContentView(cameraViewControl, layoutParamsControl);
btnCapture = (Button)findViewById(R.id.btnCapture);
btnCapture.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
camera.takePicture(cameraShutterCallback,
cameraPictureCallbackRaw,
cameraPictureCallbackJpeg);
}
});
}

CameraSource.ShutterCallback cameraShutterCallback = new CameraSource.ShutterCallback()
{
@Override
public void onShutter()
{
// TODO Auto-generated method stub
}
};

CameraSource.PictureCallback cameraPictureCallbackRaw = new CameraSource.PictureCallback()
{
@Override
public void onPictureTaken(byte[] bytes) {

}


public void onPictureTaken(byte[] data, Camera camera)
{
// TODO Auto-generated method stub
}
};

CameraSource.PictureCallback cameraPictureCallbackJpeg = new CameraSource.PictureCallback()
{
@Override
public void onPictureTaken(byte[] bytes) {

}


public void onPictureTaken(byte[] data, Camera camera)
{
// TODO Auto-generated method stub
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

wid = cameraBitmap.getWidth();
hgt = cameraBitmap.getHeight();

Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(newImage);

canvas.drawBitmap(cameraBitmap, 0f, 0f, null);

Drawable drawable = getResources().getDrawable
(R.drawable.frame);
drawable.setBounds(0, 0, wid, hgt);
drawable.draw(canvas);

File storagePath = new File(Environment.
getExternalStorageDirectory() + "/MyCameraApp/");
storagePath.mkdirs();

File myImage = new File(storagePath,
Long.toString(System.currentTimeMillis()) + ".jpg");

try
{
FileOutputStream out = new FileOutputStream(myImage);
newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


out.flush();
out.close();
}
catch(FileNotFoundException e)
{
Log.d("In Saving File", e + "");
}
catch(IOException e)
{
Log.d("In Saving File", e + "");
}

camera.startPreview();

drawable = null;

newImage.recycle();
newImage = null;

cameraBitmap.recycle();
cameraBitmap = null;
}
};

@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height)
{
// TODO Auto-generated method stub

if(previewing)
{
camera.stopPreview();
previewing = false;
}
if (camera != null){
try
{
camera.setPreviewDisplay(cameraSurfaceHolder);
camera.startPreview();
previewing = true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@Override
public void surfaceCreated(SurfaceHolder holder)
{
// TODO Auto-generated method stub
try
{
camera = Camera.open();
}
catch(RuntimeException e)
{
Toast.makeText(getApplicationContext(), "Device camera is not working properly, please try after sometime.", Toast.LENGTH_LONG).show();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}}

this is an error on CameraActivity.java

这是 cambutton.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/cambutton"
android:gravity="center_vertical" /> </LinearLayout>

这是cameraoverlay.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<SurfaceView
android:id="@+id/cameraSurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/frame" />
</LinearLayout>

希望任何人都可以修复此代码...我用android studio 3.0.1编译

最佳答案

是的,我可以修复,

我有同样的错误,您需要导入正确的相机类 (hardware.camera) ,这就是为什么要实现 Graphics 类。

删除 Camera Obj 并重新编写 And Select hardware.camera。

关于image - 在相机捕获图像 Android 上设置实时帧(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47860796/

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