Skip to main content

Custom component's surfaceCreated never called



I am trying to create a custom component to have the camera's preview. The ctr is called, but surfaceCreated is never being called. Also, the xml graphical designer doesn't complain about anything not working with the custom component.





CameraView:







import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;



import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.hardware.Camera;

import android.hardware.Camera.PreviewCallback;

import android.hardware.Camera.Size;

import android.util.AttributeSet;

import android.util.Log;

import android.view.Display;

import android.view.Surface;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.ViewGroup;

import android.view.WindowManager;



public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "Preview";

SurfaceHolder mHolder;

public Camera mCamera;

private Size mPreviewSize;

private List<Size> mSupportedPreviewSizes;

private Context mContext;

// private SurfaceView mSurfaceView;

private List<String> mSupportedFlashModes;

private boolean IsPreview = false;



public CameraView(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

setFocusable(true);

mContext = context;

mHolder = getHolder();

mHolder.addCallback(this);

mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

mHolder.setKeepScreenOn(true);

setWillNotDraw(false);

}



public void Init()

{

mCamera = Camera.open();

setCamera(mCamera);

}



/* public void surfaceCreated(SurfaceHolder holder) {

// The Surface has been created, acquire the camera and tell it where

// to draw.

camera = Camera.open();

try {

camera.setPreviewDisplay(holder);

camera.setPreviewCallback(new PreviewCallback() {

public void onPreviewFrame(byte[] data, Camera arg1) {

FileOutputStream outStream = null;

try {

outStream = new FileOutputStream(String.format(

"/sdcard/%d.jpg", System.currentTimeMillis()));

outStream.write(data);

outStream.close();

Log.d(TAG, "onPreviewFrame - wrote bytes: "

+ data.length);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {



}

Preview.this.invalidate();

}

});

} catch (IOException e) {

e.printStackTrace();

}

}

public void surfaceDestroyed(SurfaceHolder holder) {

// Surface will be destroyed when we return, so stop the preview.

// Because the CameraDevice object is not a shared resource, it's very

// important to release it when the activity is paused.

camera.stopPreview();

camera = null;

}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

// Now that the size is known, set up the camera parameters and begin

// the preview.

Camera.Parameters parameters = camera.getParameters();

parameters.setPreviewSize(w, h);

camera.setParameters(parameters);

camera.startPreview();

} */

public void surfaceDestroyed(SurfaceHolder holder)

{

// Surface will be destroyed when we return, so stop the preview.

if (mCamera != null)

{

mCamera.stopPreview();

IsPreview = false;

}

}



public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)

{

// Now that the size is known, set up the camera parameters and begin

// the preview.

if (mCamera != null)

{

Camera.Parameters parameters = mCamera.getParameters();

Size previewSize = getPreviewSize();

if (previewSize==null)

{

mCamera = null;

// Alert ("Camera not available");

}

else

{

parameters.setPreviewSize(previewSize.width, previewSize.height);



mCamera.setParameters(parameters);

mCamera.startPreview();

IsPreview = true;

}

}



}



public void surfaceCreated(SurfaceHolder holder)

{

// The Surface has been created, acquire the camera and tell it where

// to draw.

try

{

if (mCamera==null)

{

Init();

}

if (mCamera != null)

{

mCamera.setPreviewDisplay(holder);

}

}

catch (IOException exception)

{

Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);

}

}



@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

{

final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);

final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);

setMeasuredDimension(width, height);



if (mSupportedPreviewSizes != null)

{

mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);

}

}



private Size getOptimalPreviewSize(List<Size> sizes, int width, int height)

{

Size optimalSize = null;



final double ASPECT_TOLERANCE = 0.1;

double targetRatio = (double) height / width;



// Try to find a size match which suits the whole screen minus the menu on the left.

for (Size size : sizes)

{

if (size.height != width) continue;

double ratio = (double) size.width / size.height;

if (ratio <= targetRatio + ASPECT_TOLERANCE && ratio >= targetRatio - ASPECT_TOLERANCE)

{

optimalSize = size;

}

}



// If we cannot find the one that matches the aspect ratio, ignore the requirement.

if (optimalSize == null)

{

// TODO : Backup in case we don't get a size.

}



return optimalSize;

}



public void previewCamera()

{

try

{

mCamera.setPreviewDisplay(mHolder);

mCamera.startPreview();

IsPreview = true;

}

catch(Exception e)

{

Log.d(TAG, "Cannot start preview.", e);

}

}





public void setSupportedPreviewSizes(List<Size> supportedPreviewSizes)

{

mSupportedPreviewSizes = supportedPreviewSizes;

}



public Size getPreviewSize()

{

return mPreviewSize;

}



public void setCamera(Camera camera)

{

mCamera = camera;

if (mCamera != null)

{

mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();

mSupportedFlashModes = mCamera.getParameters().getSupportedFlashModes();

// Set the camera to Auto Flash mode.

if (mSupportedFlashModes!=null)

if (mSupportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO))

{

Camera.Parameters parameters = mCamera.getParameters();

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

mCamera.setParameters(parameters);

}

}

requestLayout();

}



}







Activity







/** Called when the activity is first created. */

public AssetManager assetManager;



@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// requesting to turn the title OFF

requestWindowFeature(Window.FEATURE_NO_TITLE);

// making it full screen

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

// set our MainGamePanel as the View

// InitializeGraphics2D(getWindow().getWindowStyle().getDimensionPixelSize(0, 100), getWindow().getWindowStyle().getDimensionPixelSize(1, 100));

// String Path = getBaseContext().getFilesDir().getAbsolutePath()+'/';

assetManager = getAssets();

// assetManager.open("l").read(b);



setContentView(R.layout.main);

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

try

{

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

FrameLayout frameLayout=(FrameLayout)(vi.inflate(R.layout.main, null).findViewById(R.id.frameLayout1));

// MainPanel p = new MainPanel(this, new AlertDialog.Builder(this), frameLayout);

// setContentView(p);

CameraView v=(CameraView)(frameLayout.findViewById(R.id.cameraView1));

// v.Init();

}

catch (Exception e)

{

e.printStackTrace();

}



// CameraView preview = new CameraView(this);

// frameLayout.addView(preview);

Log.d(TAG, "View added");

}




Comments

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex