我使用注释lib创建自定义ViewGroup。 我想在我的ViewGroup中的SurfaceView中加载相机。问题是,当我将我的ViewGroup放入我的布局中时,未加载摄像头(不调用SurfaceHolder.Callbacks)。
有没有办法将相机放入我的自定义视图?
我的代码在Activity中运行正常,但在我的View中它没有显示摄像头。
这是我的代码:
@EViewGroup(R.layout.activity_custom_camera)
public class TakePictureView extends RelativeLayout implements SurfaceHolder.Callback {
Context context;
Activity activity;
boolean previewing = false;
Camera camera;
@ViewById(R.id.camerapreview)
SurfaceView surfaceView;
@ViewById(R.id.button_take_picture)
ImageView takepicture;
SurfaceHolder surfaceHolder;
LayoutInflater controlInflater = null;
Camera.PictureCallback jpegCallback;
public TakePictureView(Context context, Activity activity) {
super(context);
this.context = context;
this.activity = activity;
}
public TakePictureView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void bind(){
activity.getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (surfaceHolder != null){
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
}
});
jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Toast.makeText(activity, "Picture Saved", Toast.LENGTH_SHORT).show();
}
};
takepicture.setClickable(true);
takepicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takePicture();
}
});
}
void takePicture(){
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.takePicture(null, null, jpegCallback);
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d("CAMERA", "CRIADA");
camera = Camera.open();
Log.d("CAMERA", "CRIADA" + camera.getParameters().toString());
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d("CAMERA", "SURFACE CHANGED");
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
Camera.Parameters parameters = camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setDisplayOrientation(90);
parameters.setRotation(90);
}
else {
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
// For Android 2.2 and above
camera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
答案 0 :(得分:0)
嗨而不是扩展视图组,你应该扩展SurfaceView&在活动和布局中使用自定义类。看下面的代码,它一定会帮到你。
Packager com.test.cam;
public class AdvancePreview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mSurfaceHolder;
public Camera mCamera;
public Activity mActivity;
public AdvancePreview(Context context) {
super(context);
}
public AdvancePreview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setUpSurface(Activity activity){
try
{
mActivity = activity;
this.mSurfaceHolder = getHolder();
this.mSurfaceHolder.setFormat(PixelFormat.JPEG);
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
catch(Exceptions e){
e.printStackTrace();
}
}
private SurfaceHolder getSurfaceHolder(){
return this.mSurfaceHolder;
}
// SurfaceHolder.Callback
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.print(this.getClass().toString(), "surfaceCreated()");
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(getSurfaceHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
// SurfaceHolder.Callback
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Parameters params = null;
Log.print(this.getClass().toString(), "surfaceChanged()");
try {
if (camera != null) {
params = mCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
mCamera.setParameters(params);
mCamera.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// SurfaceHolder.Callback
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
mCamera.stopPreview();
mCamera.release();
} catch (Exception e) {
e.printStackTrace();
}
mCamera = null;
}
public void startTakePicture() {
Parameters params = null;
AudioManager mgr = null;
Log.debug(this.getClass().toString(), "startTakePictre()");
try {
mgr = (AudioManager)Const.CONTEXT.getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
if (mCamera != null){
params = mCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
mCamera.setParameters(params);
mCamera.autoFocus(new CustomAutoFoucsCallback(activity));
}else{
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
下一步如何在您的活动中使用此AdvancePreview类,请参阅以下带有活动的代码及其布局xml文件。
Packager com.test.cam;
public class CamActivity extends Activity {
public AdvancePreview mAdvancePreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cam_activity);
mAdvancePreview = (AdvancePreview)findViewById(R.id.advancepreview);
mAdvancePreview.setUpSurface(this);
}
// class this function on click event of button
public void takePicture(){
mAdvancePreview.startTakePicture();
}
}
cam_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.test.cam.AdvancePreview
android:id="@+id/advancepreview"
android:layout_width="1dp"
android:layout_height="1dp"
/>
</LinearLayout>
对于保存捕获的照片保存使用CustomAutoFocusCallback,请参阅AdvancePreview的以下行
camera.autoFocus(new CustomAutoFoucsCallback(activity));
如果您有任何问题或疑问,请与我们联系。谢谢