Immagine a camma sul supporto superficiale, disegno: eccezione a causa del tipo di superficie
-
27-10-2019 - |
Domanda
Sto usando un SurfaceView
con un SurfaceHolder
Per iniziare con un'anteprima della fotocamera nella mia app di test.
public class TextLocatorActivity extends Activity {
private Preview pvw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
pvw = new Preview(this);
setContentView(pvw);
}
Voglio usare CameraPreview (viene fornito con i campioni SDK per SDK versione 7). Un clic sull'interfaccia utente fa una foto. Ecco il Preview
classe:
public class Preview extends SurfaceView implements OnClickListener, SurfaceHolder.Callback {
SurfaceHolder holder;
Camera cam;
final static String TAG = "TextLocator:Preview";
Preview(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
this.setOnClickListener(this);
// seems to be required (although the docs state, this enum is deprecated, as the type will be chosen automatically...
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
cam = Camera.open();
try {
Camera.Parameters params = cam.getParameters();
params.set("orientation", "landscape");
Camera.Size bestSize = getBestPreviewSize(480, 320);
params.setPreviewSize(bestSize.width, bestSize.height);
cam.setParameters(params);
// where to draw:
cam.setPreviewDisplay(holder);
} catch (IOException exception) {
cam.release();
cam = null;
// TODO: add more exception handling logic here
}
}
private Camera.Size getBestPreviewSize(int width, int height)
{
// checks for supported sizes close to the demanded values - implemented.
}
public void surfaceDestroyed(SurfaceHolder holder) {
cam.stopPreview();
cam.release();
cam = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = cam.getParameters();
parameters.setPreviewSize(w, h);
cam.setParameters(parameters);
cam.startPreview();
}
Camera.PictureCallback picCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] bytes, Camera arg1) {
synchronized(holder) {
Canvas c = null;
try {
c = holder.lockCanvas();
Paint paint = new Paint();
paint.setAntiAlias(false);
paint.setARGB(200, 120, 180, 0);
c.drawLine(10, 10, c.getWidth() - 10, c.getHeight() - 10, paint);
}
catch (Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
// IllegalArguementException Surface Type is SURFACE_TYPE_PUSH_BUFFERS
}
finally {
holder.unlockCanvasAndPost(c);
}
}
}
};
public void onClick(View v)
{
cam.takePicture(null, null, picCallback);
}
}
Successivamente sto cercando di fare un po 'di dipinto aggiuntivo al corrispondente Canvas
del SurfaceHolder
. Quindi sto chiamando canvas = holder.lockCanvas()
. Ciò si tradurrà in un IllegalArgumentException
con il messaggio:
Surface type is SURFACE_TYPE_PUSH_BUFFERS
Adesso, Documenti Indicare che quei tipi di superficie sono deprecati, il set di valore verrà ignorato. Tuttavia, l'anteprima della fotocamera funziona solo, se imposto il tipo su questo valore specifico.
Come posso ottenere il disegno sul Canvas
del SurfaceView
L'immagine scattata viene visualizzata? O dovrebbe essere messo su un livello/vista diverso?
Soluzione
Non è possibile visualizzare l'anteprima della fotocamera e disegnare con una tela. Devi fare l'uno o l'altro.