问题描述
我想实现放大和放大器;在我的自定义相机功能。
NullPointerException异常:zoomControls.setVisibility(View.GONE);
我使用这样的:Zoom控制摄像头不工作在Android的
previewSurface.java: -
公共类previewSurface延伸SurfaceView工具
SurfaceHolder.Callback {
公共静态最后弦乐LOG_TAG =相机preVIEW;
私人SurfaceHolder mSurfaceHolder;
SurfaceView mSurfaceView;
私人相机mCamera;
Camera.Parameters PARAMS;
INT currentZoomLevel = 0,maxZoomLevel = 0;
//构造,其获得的上下文和摄像头
@燮pressWarnings(德precation)
公共previewSurface(上下文的背景下,相机摄像头){
超(上下文);
this.mCamera =摄像头;
mSurfaceView =(SurfaceView)findViewById(R.id.surfaceView);
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(本);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.mSurfaceHolder.setFixedSize(100,100);
}
@覆盖
公共无效surfaceCreated(SurfaceHolder surfaceHolder){
尝试 {
mCamera.set previewDisplay(surfaceHolder);
mCamera.start preVIEW();
}赶上(IOException异常E){
//留空现在
}
}
@覆盖
公共无效surfaceDestroyed(SurfaceHolder surfaceHolder){
mCamera.stop preVIEW();
mCamera.release();
}
@覆盖
公共无效surfaceChanged(SurfaceHolder surfaceHolder,INT格式,
INT宽度,高度INT){
ZoomControls zoomControls =(ZoomControls)findViewById(R.id.CAMERA_ZOOM_CONTROLS);
PARAMS = mCamera.getParameters();
如果(params.isZoomSupported()&安培;&安培; params.isSmoothZoomSupported()){
//大多数手机
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(真正的);
zoomControls.setIsZoomOutEnabled(真正的);
zoomControls.setOnZoomInClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(currentZoomLevel< maxZoomLevel){
currentZoomLevel ++;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(currentZoomLevel大于0){
currentZoomLevel--;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
}否则如果(params.isZoomSupported()&安培;&安培;!params.isSmoothZoomSupported()){
//愚蠢的HTC手机
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(真正的);
zoomControls.setIsZoomOutEnabled(真正的);
zoomControls.setOnZoomInClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(currentZoomLevel< maxZoomLevel){
currentZoomLevel ++;
params.setZoom(currentZoomLevel);
mCamera.setParameters(PARAMS);
}
}
});
zoomControls.setOnZoomOutClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(currentZoomLevel大于0){
currentZoomLevel--;
params.setZoom(currentZoomLevel);
mCamera.setParameters(PARAMS);
}
}
});
}其他{
//对手机没有放大
zoomControls.setVisibility(View.GONE);
}
//启动preVIEW使用新设置
尝试 {
mCamera.setDisplayOrientation(90);
mCamera.set previewDisplay(surfaceHolder);
mCamera.start preVIEW();
}赶上(例外五){
//有意留空白测试
}
}
}
activity_camera.xml: -
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
<的FrameLayout
机器人:ID =@ + ID / camera_ preVIEW
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_toLeftOf =@ + ID / btnView/>
< SurfaceView
机器人:ID =@ + ID / surfaceView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
< ZoomControls
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID / camera_ preVIEW
机器人:layout_centerInParent =真
机器人:ID =@ + ID / CAMERA_ZOOM_CONTROLS/>
<的TextView
机器人:ID =@ + ID /标题
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_marginTop =18dp
机器人:layout_toRightOf =@ + ID / camera_ preVIEW
机器人:MAXLINES =1
机器人:能见度=水涨船高
机器人:textAppearance =:/>中的Android ATTR / textAppearanceSmall?
<按钮
机器人:ID =@ + ID / btnSettings
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / btnView
机器人:layout_alignParentBottom =真
机器人:layout_marginBottom =50dp
机器人:背景=@可绘制/设置/>
<按钮
机器人:ID =@ + ID / btnView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_alignParentTop =真
机器人:layout_marginTop =50dp
机器人:背景=@可绘制/画廊/>
<按钮
机器人:ID =@ + ID / btnCapture
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerVertical =真
机器人:layout_toRightOf =@ + ID / camera_ preVIEW
机器人:背景=@可绘制/捕获/>
< / RelativeLayout的>
CameraLauncherActivity.java: -
公共类CameraLauncherActivity延伸活动{
私人相机mCamera;
私人previewSurface mCamera preVIEW;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mCamera preVIEW =新的previewSurface(这一点,mCamera);
的FrameLayout preVIEW =(的FrameLayout)findViewById(R.id.camera_ preVIEW);
preview.addView(mCamera preVIEW);
}
私人摄像机getCameraInstance(){
摄像头摄像头= NULL;
Log.d(CameraLauncherActivity.LOG_TAG,getCameraInstance()照相机:+摄像头);
尝试 {
相机= Camera.open(0);
Log.d(CameraLauncherActivity.LOG_TAG,getCameraInstance()开::+摄像头);
}赶上(例外五){
//不能得到相机或不存在
}
返回摄像头;
}
PictureCallback mPicture =新PictureCallback(){
@覆盖
公共无效onPictureTaken(byte []的数据,摄像头摄像头){
文件PictureFile的= getOutputMediaFile();
camera.start preVIEW();
如果(PictureFile的== NULL){
返回;
}
尝试 {
FileOutputStream中FOS =新的FileOutputStream(PictureFile的);
fos.write(数据);
fos.close();
}赶上(FileNotFoundException异常E){
}赶上(IOException异常E){
}
}
};
静态文件getOutputMediaFile(){
//文件夹名称
mediaStorageDir =新的文件(环境.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),/ MyImages /);
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
Log.d(应用程序,无法创建目录);
}
}
/ * YYYY-MM-dd'T'HH:毫米:ss.SSSZ * /
字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date())。
文件媒体文件;
// 文件名
媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+IMG_+的timeStamp +.JPG);
返回媒体文件;
}
的logcat: -
1月11日至28日:52:43.179:E / AndroidRuntime(2135):致命异常:主要
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):显示java.lang.NullPointerException
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在app.pd. previewSurface.surfaceChanged(previewSurface.java:113)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.SurfaceView.updateWindow(SurfaceView.java:582)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.SurfaceView.access $ 000(SurfaceView.java:86)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.SurfaceView $ 3.on preDraw(SurfaceView.java:175)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.ViewTreeObserver.dispatchOn preDraw(ViewTreeObserver.java:833)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1860)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.Choreographer.doCallbacks(Choreographer.java:562)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.Choreographer.doFrame(Choreographer.java:532)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.os.Handler.handleCallback(Handler.java:730)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.os.Handler.dispatchMessage(Handler.java:92)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.os.Looper.loop(Looper.java:137)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在android.app.ActivityThread.main(ActivityThread.java:5103)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在java.lang.reflect.Method.invokeNative(本机方法)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在java.lang.reflect.Method.invoke(Method.java:525)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
1月十一日至28日:52:43.179:E / AndroidRuntime(2135):在dalvik.system.NativeStart.main(本机方法)
在你的code, PARAMS
未初始化过,因此它的值总是空。这将导致 NullPointerException异常
如果您尝试空物体上使用的方法。也许在你的构造函数,你应该用下面的初始化:
PARAMS = mCamera.getParameters();
I am trying to Implement Zoom In & Out functionality in my Custom Camera.
NullPointerException: zoomControls.setVisibility(View.GONE);
I am using this : Zoom Control in Camera is not working in Android
PreviewSurface.java:-
public class PreviewSurface extends SurfaceView implements
SurfaceHolder.Callback {
public static final String LOG_TAG = "CameraPreview";
private SurfaceHolder mSurfaceHolder;
SurfaceView mSurfaceView;
private Camera mCamera;
Camera.Parameters params ;
int currentZoomLevel = 0, maxZoomLevel = 0;
// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public PreviewSurface(Context context, Camera camera) {
super(context);
this.mCamera = camera;
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.mSurfaceHolder.setFixedSize(100, 100);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// left blank for now
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.CAMERA_ZOOM_CONTROLS);
params = mCamera.getParameters();
if (params.isZoomSupported() && params.isSmoothZoomSupported()) {
//most phones
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
mCamera.startSmoothZoom(currentZoomLevel);
}
}
});
} else if (params.isZoomSupported() && !params.isSmoothZoomSupported()){
//stupid HTC phones
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
params.setZoom(currentZoomLevel);
mCamera.setParameters(params);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
params.setZoom(currentZoomLevel);
mCamera.setParameters(params);
}
}
});
}else{
//no zoom on phone
zoomControls.setVisibility(View.GONE);
}
// start preview with new settings
try {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (Exception e) {
// intentionally left blank for a test
}
}
}
activity_camera.xml:-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/btnView" />
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/camera_preview"
android:layout_centerInParent="true"
android:id="@+id/CAMERA_ZOOM_CONTROLS" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:layout_toRightOf="@+id/camera_preview"
android:maxLines="1"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnView"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:background="@drawable/settings" />
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:background="@drawable/gallery" />
<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/camera_preview"
android:background="@drawable/capture" />
</RelativeLayout>
CameraLauncherActivity.java:-
public class CameraLauncherActivity extends Activity {
private Camera mCamera;
private PreviewSurface mCameraPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mCameraPreview = new PreviewSurface(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
}
private Camera getCameraInstance() {
Camera camera = null;
Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()Camera:: " + camera);
try {
camera = Camera.open(0);
Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()open:: " + camera);
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
camera.startPreview();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
static File getOutputMediaFile() {
// folder name
mediaStorageDir = new File(Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/MyImages/");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("App", "failed to create directory");
}
}
/* yyyy-MM-dd'T'HH:mm:ss.SSSZ */
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
// file name
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
Logcat:-
11-28 01:52:43.179: E/AndroidRuntime(2135): FATAL EXCEPTION: main
11-28 01:52:43.179: E/AndroidRuntime(2135): java.lang.NullPointerException
11-28 01:52:43.179: E/AndroidRuntime(2135): at app.pd.PreviewSurface.surfaceChanged(PreviewSurface.java:113)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.SurfaceView.access$000(SurfaceView.java:86)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:833)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1860)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.os.Handler.handleCallback(Handler.java:730)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.os.Looper.loop(Looper.java:137)
11-28 01:52:43.179: E/AndroidRuntime(2135): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 01:52:43.179: E/AndroidRuntime(2135): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 01:52:43.179: E/AndroidRuntime(2135): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 01:52:43.179: E/AndroidRuntime(2135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 01:52:43.179: E/AndroidRuntime(2135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 01:52:43.179: E/AndroidRuntime(2135): at dalvik.system.NativeStart.main(Native Method)
In your code, params
isn't ever initialized, thus its value is always null. This will result in a NullPointerException
if you try to use a method on the null object. Perhaps in your constructor you should initialize it with the following:
params = mCamera.getParameters();
这篇关于放大和放大器;出定制的相机 - 空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!