5 / 5 / 0
Регистрация: 02.01.2013
Сообщений: 438
|
|
1
|
Не срабатывает событие surfaceChanged
21.07.2017, 21:37. Показов 353. Ответов 0
Скажите пожалуйста почему у меня не срабатывает событие surfaceChanged?
MainActivity :
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
| public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
static final Integer CAMERA = 0x1;
static final Integer WRITE_EXST = 0x2;
static final Integer READ_EXST = 0x3;
SurfaceView camView; // drawing camera preview using this variable
SurfaceHolder surfaceHolder; // variable to hold surface for surfaceView which means display
boolean camCondition = false; // conditional variable for camera preview checking and set to false
Button cap; // image capturing button
public Camera camera; // camera class variable
int height;
int width;
boolean mBounded;
ShotService mServer;
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, ShotService.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
};
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MainActivity.this, "Service is disconnected", Toast.LENGTH_SHORT).show();
mBounded = false;
mServer = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this, "Service is connected", Toast.LENGTH_SHORT).show();
mBounded = true;
ShotService.LocalBinder mLocalBinder = (ShotService.LocalBinder)service;
mServer = mLocalBinder.getServerInstance();
camera = mServer.camera;
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
getWindow().setFormat(PixelFormat.UNKNOWN);
camView = (SurfaceView) findViewById(R.id.camerapreview);
askForPermission(Manifest.permission.CAMERA,CAMERA);
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,WRITE_EXST);
askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE,READ_EXST);
// getWindow() to get window and set it's pixel format which is UNKNOWN
getWindow().setFormat(PixelFormat.UNKNOWN);
// refering the id of surfaceView
camView = (SurfaceView) findViewById(R.id.camerapreview);
// refering button id
cap = (Button) findViewById(R.id.button2);
// getting access to the surface of surfaceView and return it to surfaceHolder
surfaceHolder = camView.getHolder();
// adding call back to this context means MainActivity
surfaceHolder.addCallback(MainActivity.this);
// to set surface type
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
// click event on button
cap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
};
@Override
protected void onStop() {
super.onStop();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int value;
public void surfaceCreated(SurfaceHolder surfaceHolder) {
value = this.getResources().getConfiguration().orientation;
if (value == Configuration.ORIENTATION_PORTRAIT) {
//orientation = "Portrait";
camera.setDisplayOrientation(90); // setting camera preview orientation
}
if (value == Configuration.ORIENTATION_LANDSCAPE) {
camera.setDisplayOrientation(0); // setting camera preview orientation
//orientation = "Landscape";
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
// stop the camera
if(camCondition){
camera.stopPreview(); // stop preview using stopPreview() method
camCondition = false; // setting camera condition to false means stop
}
// condition to check whether your device have camera or not
if (camera != null){
try {
Camera.Parameters parameters = camera.getParameters();
//parameters.setColorEffect(Camera.Parameters.FOCUS_MODE_AUTO); //applying effect on camera
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(parameters); // setting camera parameters
camera.setPreviewDisplay(surfaceHolder); // setting preview of camera
camera.startPreview(); // starting camera preview
camCondition = true; // setting camera to true which means having camera
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} |
|
И собственно сам Service :
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
public class ShotService extends Service {
final String LOG_TAG = "myLogs";
public Camera camera; // camera class variable
public boolean pause;
Integer degrees = 0;
private int rotate;
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LOG_TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy");
}
IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
camera = Camera.open(); // opening camera
//STEP #1: Get rotation degrees
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break; //Natural orientation
case Surface.ROTATION_90: degrees = 90; break; //Landscape left
case Surface.ROTATION_180: degrees = 180; break;//Upside down
case Surface.ROTATION_270: degrees = 270; break;//Landscape right
}
rotate = (info.orientation - degrees + 360) % 360;
return mBinder;
}
public class LocalBinder extends Binder {
public ShotService getServerInstance() {
return ShotService.this;
}
}
public void start() {
Runnable runnable = new Runnable() {
public void run() {
while (pause == false) {
Camera.Parameters params = camera.getParameters();
params.setRotation(rotate);
camera.setParameters(params);
// calling a method of camera class takepicture by passing one picture callback interface parameter
camera.takePicture(null, null, null, mPictureCallback);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public Integer imgcount=0;
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
FileOutputStream outStream = null;
try {
// Directory and name of the photo. We put system time
// as a postfix, so all photos will have a unique file name.
outStream = new FileOutputStream("/sdcard/new_" + (imgcount++).toString() +".jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "shorted", Toast.LENGTH_LONG).show();
}
}); }
}
};
public void Focus() {
camera.autoFocus(null);
}
} |
|
0
|