Hye there I'm new to OpenCV but somehow good at Android! I have edited a code to change the Camera Background. For the Image to Set at the Background; I have been successfully done with it! Now the problem is that the ImageView comes over the camera screen! I just want my ImageView to come under the Camera I mean at the back of my camera! My code that I'm using is:
import android.util.Log;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import java.util.Random;
public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
private static final String TAG = "Svetlin SurfaceView";
private ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.custom, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
image=(ImageView)findViewById(R.id.myimage);
image.setAlpha(150);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
Camera.Parameters params = camera.getParameters();
params.setSceneMode(Camera.Parameters.SCENE_MODE_SUNSET);
//ImageView overlay = (ImageView) findViewById(R.id.ImageView01);
//overlay.bringToFront();
camera.setParameters(params);
// tryDrawing(holder);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
private void tryDrawing(SurfaceHolder holder) {
Log.i(TAG, "Trying to draw...");
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
drawMyStuff(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas) {
Random random = new Random();
Log.i(TAG, "Drawing...");
canvas.drawRGB(255, 128, 128);
}
}
The White Image Should Come Behind the Camera as you can see in the screen shot!
The thing is I just need to know that how can I change the Camera Background doing so! Please give me Hints or any Idea so that can help me. THANKS IN ADVANCE!
Related
I'm a noobie in android dev and I'm having a issue. I'm trying to make a simple game using SurfaceView. But surfaceCreated is never called when I'm launching the app.
I'm using his answer to use less ram with the background
Here is where the setcontentView is placed:
call_surfaceView = new class_surfaceView(cMainActivity);
// Setup your SurfaceView
SurfaceView surfaceView = call_surfaceView; // use any SurfaceView you want
surfaceView.setZOrderOnTop(true);
surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
// Setup your ImageView
ImageView bgImagePanel = new ImageView(cMainActivity);
bgImagePanel.setBackgroundResource(R.drawable.background); // use any Bitmap or BitmapDrawable you want
// Use a RelativeLayout to overlap both SurfaceView and ImageView
RelativeLayout.LayoutParams fillParentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rootPanel = new RelativeLayout(cMainActivity);
rootPanel.setLayoutParams(fillParentLayout);
rootPanel.addView(bgImagePanel, fillParentLayout);
rootPanel.addView(surfaceView, fillParentLayout);
setContentView(rootPanel);
and here is my surfaceView code:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class class_surfaceView extends SurfaceView implements SurfaceHolder.Callback {
Thread t;
Canvas c;
SurfaceHolder holder;
Bitmap ghost;
Bitmap lifebar;
Bitmap player;
Bitmap sol;
Bitmap tombe;
public class_surfaceView(Context context) {
super(context);
holder = getHolder();
Log.d("Essai", "Class_surfaceView");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("Essai", "surfaceCreated");
new Thread(new Runnable() {
#Override
public void run() {
lauch_game();
}
}).start();
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
public void lauch_game() {
ghost = BitmapFactory.decodeResource(getResources(),R.drawable.ghost);
c = holder.lockCanvas();
if(c != null){
c.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawBitmap(ghost,0,0,null);
holder.unlockCanvasAndPost(c);
} else {
Log.d("DEBUG","c is null");
}
Log.d("Essai", "Essais de dessin");
}
}
Thanks for helping !
In your class_surfaceView constructor after holder = getHolder(), you must call holder.addCallback(this) to register the callback.
Try to use #Override above method
I have a "Board.java" class, which is an activity, and a "MySurfaceView.java", which is a SurfaceView. I'm drawing some bitmap on the SurfaceView and I want to move it when the screen is touched (to the touched place). it draws the bitmap but after touching, nothing happened.
Thanks in advance!
Log:
Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=539.47266, y[0]=978.45703, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=90656, downTime=88761, deviceId=0, source=0x1002 }
Board.java:
package com.myfirstapplication.owner.appversion1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Owner on 15/06/2016.
*/
public class Board extends AppCompatActivity implements View.OnTouchListener {
TextView tv;
MySurfaceView mv;
Bitmap bp;
float x, y;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
mv.setOnTouchListener(this);
bp = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
x = 0;
y = 0;
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
x = event.getX();
y = event.getY();
return true;
}
}
MySurfaceView.java:
package com.myfirstapplication.owner.appversion1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Owner on 16/06/2016.
*/
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}
Remove TouchListener in Board
Board:
public class Board extends AppCompatActivity{
MySurfaceView mv;
Bitmap bp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
bp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
}
And in MySurfaceView
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
// run();
return super.onTouchEvent(event);
}
}
Above work fine for me.
I have a "Board.java" class, which is an activity, and a "MySurfaceView.java", which is a SurfaceView. I'm drawing some bitmap on the SurfaceView and I want to move it when the screen is touched (to the touched place). it draws the bitmap but after touching, nothing happened.
Thanks in advance!
Log:
Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=539.47266, y[0]=978.45703, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=90656, downTime=88761, deviceId=0, source=0x1002 }
Board.java:
package com.myfirstapplication.owner.appversion1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Owner on 15/06/2016.
*/
public class Board extends AppCompatActivity implements View.OnTouchListener {
TextView tv;
MySurfaceView mv;
Bitmap bp;
float x, y;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
mv.setOnTouchListener(this);
bp = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
x = 0;
y = 0;
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
x = event.getX();
y = event.getY();
return true;
}
}
MySurfaceView.java:
package com.myfirstapplication.owner.appversion1;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Owner on 16/06/2016.
*/
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}
Remove TouchListener in Board
Board:
public class Board extends AppCompatActivity{
MySurfaceView mv;
Bitmap bp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
bp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
}
And in MySurfaceView
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
// run();
return super.onTouchEvent(event);
}
}
Above work fine for me.
I saw this one from here. And I just want to try if it is working.
Link: http://www.javacodegeeks.com/2011/03/android-http-camera-live-preview.html
This is the first class called MyCamAppActivity.java
package com.javacodegeeks.android.camera;
import android.app.Activity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Window;
public class MyCamAppActivity extends Activity {
private SurfaceView cameraPreview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
cameraPreview = new CameraPreview(this);
setContentView(cameraPreview);
}
}
And this is the 2nd class called the CameraPreview
package com.javacodegeeks.android.camera;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Camera camera;
public CameraPreview(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceChanged(SurfaceHolder holder2, int format, int w, int h) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
camera.setParameters(parameters);
camera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder1) {
try {
camera = Camera.open();
camera.setPreviewDisplay(holder1);
}
catch (Exception e) {
Log.i("Exception surfaceCreated()", "e=" + e);
camera.release();
camera = null;
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
camera = null;
}
}
As far as I know, when you use surfaceview in android programming it automatically generated the picture on your screen. But in my case, it's just plain black picture. Btw, the complete package in the site doesn't have any .xml contents in it. So, I don't know if I still have to fix something in the xml. Hope someone or any of you could help me ASAP.
i have a surface view and i wish "draw" the content of a linear layout on my surface view.
My surface view is the preview of the camera, i wish when i take the picture the content of my layout is drawn on the picture.
How do this?
Thx.
Surface layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/takepicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/trans_tshirt"
android:contentDescription="tshirt" />
main.xml
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<SurfaceView android:layout_height="match_parent" android:layout_width="match_parent" android:id="#+id/camerapreview"/>
</LinearLayout>
java code:
package com.views;
import android.app.Activity;
import android.content.ContentValues;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
import com.fashion.alex.R;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
public class HomeActivity extends Activity implements SurfaceHolder.Callback
{
private Camera camera;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private boolean previewing = false;
private LayoutInflater controlInflater = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.tshirt_layout, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
final ImageView ivButton = (ImageView)findViewById(R.id.takepicture);
ivButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}}
);
}
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){
#Override
public void onShutter() {
}
};
Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){
#Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}
};
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){
#Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(HomeActivity.this,
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
};
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
camera = null;
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
create a Layout/.xml file..like below
1) LinearLayout A
2) Linear Layout A must have child views which may be BUtton/text whatever you want.
3) Linear Layout A must also have a surfaceview B
use this surfaceView B in your activity to show camera preview. in this way you can basically have surface view and other views on the screen.