gpt4 book ai didi

java - 如何从 java android 中的相机预览中获取字节数组?

转载 作者:行者123 更新时间:2023-11-30 08:54:26 24 4
gpt4 key购买 nike

我是个彻头彻尾的菜鸟。阅读此 http://developer.android.com/guide/topics/media/camera.html 后,我设法编写并理解了这段代码.但现在我想获取字节数组进行预览,然后将其转换为位图。但我想实时执行此操作而不必被迫在存储中保存图片文件。请帮忙!这是我的程序代码。

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);

mCamera.startPreview();
} catch (IOException e) {
String TAG = null;
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.


mCamera.setDisplayOrientation(90);
// set preview size and make any resize, rotate or
// reformatting changes here

// start preview with new settings
try {

mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){
String TAG = null;
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
private PictureCallback mPicture = new PictureCallback(){

@Override
public void onPictureTaken(byte[] data, Camera camera)

{
// TODO: Implement this method

}
};

}

主要 Activity :

public class MainActivity extends Activity
{ private Camera mCamera;
private CameraPreview mPreview;
int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);

FrameLayout preview = (FrameLayout) findViewById(R.id.frame);
preview.addView(mPreview);



}
public static Camera getCameraInstance()
{
Camera c = null;
try
{
c = Camera.open();}
catch (Exception e)
{ System.out.println("blamjjjh");}
return c;
}
public void releasec(){
mCamera.release();
}

@Override
protected void onStop()
{

super.onStop();
releasec();
}
}

最佳答案

详细in the Android Developer docs here (您可能已经阅读过),将 PictureCallback 接口(interface)的实现(参见下面的示例)添加到您的 Activity。您也可以使用 BitmapFactory 将返回的字节数组转换为 Bitmap。然后您可以根据需要使用它。

注意:我也会阅读 the docs here关于有效处理与内存相关的位图,因为如果您正在操作位图,您可能会遇到 OutOfMemory 错误。

private PictureCallback mPicture = new PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

//create a Bitmap from the byte array
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

//use your Bitmap
}
};

然后您需要将其传递给相机实例的 takePicture() 方法,例如

// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);

希望对您有所帮助! :-)

关于java - 如何从 java android 中的相机预览中获取字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394536/

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