【Android】快速切换到主线程更新UI的几种方法
方法一: view.post(Runnable action)
textView.post(new Runnable() {
@Override
public void run() {
textView.setText("更新textView");
//还可以更新其他的控件
imageView.setBackgroundResource(R.drawable.update);
}
});
方法二: activity.runOnUiThread(Runnable action)
public void updateUI(final Context context) {
((MainActivity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
//此时已在主线程中,可以更新UI了
}
});
}
方法三: Handler机制
...
2018年9月12日水曜日
2018年7月19日木曜日
java_Semaphore
java_Semaphore
import java.util.concurrent.Semaphore;
import java.lang.System;
class M180717_Semaphore {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(8);
long s = System.currentTimeMillis();
A a = new A(semaphore);
a.start();
B b = new B(semaphore);
b.start();
B b2 = new B(semaphore);
b2.start();
}
}
class A extends Thread {
private Semaphore semaphore;
public A(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "-------");
for (int i = 0 ; i < 99 ; i++) {
System.out.print("a");
}
semaphore.release();
} catch (Exception e) {
System.out.println(semaphore.getQueueLength());
}
}
}
class B extends Thread {
private Semaphore semaphore;
public B(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void run() {
try {
// System.out.println(semaphore.getQueueLength());
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "-------");
for (int i = 0 ; i < 99 ; i++) {
System.out.print("b");
}
semaphore.release();
} catch (Exception e) {
System.out.println(semaphore.getQueueLength());
}
}
}
class C extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-------");
for (int i = 0 ; i < 99 ; i++) {
System.out.print("c");
}
}
}
class X implements Runnable {
@Override
public void run() {
System.out.println("xxxxx");
}
}
import java.util.concurrent.Semaphore;
import java.lang.System;
class M180717_Semaphore {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(8);
long s = System.currentTimeMillis();
A a = new A(semaphore);
a.start();
B b = new B(semaphore);
b.start();
B b2 = new B(semaphore);
b2.start();
}
}
class A extends Thread {
private Semaphore semaphore;
public A(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "-------");
for (int i = 0 ; i < 99 ; i++) {
System.out.print("a");
}
semaphore.release();
} catch (Exception e) {
System.out.println(semaphore.getQueueLength());
}
}
}
class B extends Thread {
private Semaphore semaphore;
public B(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void run() {
try {
// System.out.println(semaphore.getQueueLength());
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "-------");
for (int i = 0 ; i < 99 ; i++) {
System.out.print("b");
}
semaphore.release();
} catch (Exception e) {
System.out.println(semaphore.getQueueLength());
}
}
}
class C extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-------");
for (int i = 0 ; i < 99 ; i++) {
System.out.print("c");
}
}
}
class X implements Runnable {
@Override
public void run() {
System.out.println("xxxxx");
}
}
Android Handler Thread HandlerThread
Android Handler Thread HandlerThread
Handler.Callback callback = new Handler.Callback()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
CC();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessage(99);
}
});
}
protected void CC() {
HandlerThread handlerThread = new HandlerThread("test");
handlerThread.start();
Handler.Callback callback = new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
System.out.println();
System.out.println(Thread.currentThread().getName());
System.out.println("0000000000000000000000000000000000000");
return false;
}
};
handler = new Handler(handlerThread.getLooper(), callback);
}
Handler.Callback callback = new Handler.Callback()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
CC();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessage(99);
}
});
}
protected void CC() {
HandlerThread handlerThread = new HandlerThread("test");
handlerThread.start();
Handler.Callback callback = new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
System.out.println();
System.out.println(Thread.currentThread().getName());
System.out.println("0000000000000000000000000000000000000");
return false;
}
};
handler = new Handler(handlerThread.getLooper(), callback);
}
Android Handler Thread HandlerThread
Android Handler Thread HandlerThread
從主線程發送消息到子線程。
子線程必須創建Looper,
handlerThread.getLooper(),
Looper.prepare(),Looper.loop()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
Tthread tthread = new Tthread();
tthread.start();
T3();
T4();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hh.sendEmptyMessage(12);
handler.sendEmptyMessage(12);
handlerT4.sendEmptyMessage(12);
}
});
}
protected void T3() {
HandlerThread handlerThread = new HandlerThread("name-thread");
handlerThread.start();
handler = new Handler(handlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println(Thread.currentThread().getName());
System.out.println("T33333333333333333");
}
};
}
protected void T4() {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
Looper.prepare();
handlerT4 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("T44444444444444444444");
}
};
Looper.loop();
}
};
thread.start();
}
class Tthread extends Thread {
@Override
public void run() {
super.run();
Looper.prepare();
hh = new HH();
Looper.loop();
}
}
class HH extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("HHHHHHHHHHHHHHHHHHHHHHHH");
System.out.println(Thread.currentThread().getName());
}
}
從主線程發送消息到子線程。
子線程必須創建Looper,
handlerThread.getLooper(),
Looper.prepare(),Looper.loop()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
Tthread tthread = new Tthread();
tthread.start();
T3();
T4();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hh.sendEmptyMessage(12);
handler.sendEmptyMessage(12);
handlerT4.sendEmptyMessage(12);
}
});
}
protected void T3() {
HandlerThread handlerThread = new HandlerThread("name-thread");
handlerThread.start();
handler = new Handler(handlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println(Thread.currentThread().getName());
System.out.println("T33333333333333333");
}
};
}
protected void T4() {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
Looper.prepare();
handlerT4 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("T44444444444444444444");
}
};
Looper.loop();
}
};
thread.start();
}
class Tthread extends Thread {
@Override
public void run() {
super.run();
Looper.prepare();
hh = new HH();
Looper.loop();
}
}
class HH extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("HHHHHHHHHHHHHHHHHHHHHHHH");
System.out.println(Thread.currentThread().getName());
}
}
2018年7月15日日曜日
StreamConfigurationMap
StreamConfigurationMap
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics("0");
StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] a = streamConfigurationMap.getOutputSizes(MediaRecorder.class);
Size[] b = streamConfigurationMap.getOutputSizes(SurfaceHolder.class);
System.out.println(streamConfigurationMap);
07-15 01:25:52.463 7881-7881/? I/System.out:
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics("0");
StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] a = streamConfigurationMap.getOutputSizes(MediaRecorder.class);
Size[] b = streamConfigurationMap.getOutputSizes(SurfaceHolder.class);
System.out.println(streamConfigurationMap);
07-15 01:25:52.463 7881-7881/? I/System.out:
StreamConfiguration(Outputs(
[w:4096, h:3072, format:JPEG(256), min_duration:0, stall:1093386752],
[w:4096, h:2304, format:JPEG(256), min_duration:0, stall:870040064],
[w:3648, h:2736, format:JPEG(256), min_duration:0, stall:908645888],
[w:3648, h:2052, format:JPEG(256), min_duration:0, stall:731484416],
[w:3264, h:2448, format:JPEG(256), min_duration:0, stall:767309312],
[w:3264, h:1836, format:JPEG(256), min_duration:0, stall:625481984],
[w:2592, h:1944, format:JPEG(256), min_duration:0, stall:557758208],
[w:2592, h:1458, format:JPEG(256), min_duration:0, stall:468318656],
[w:2080, h:1560, format:JPEG(256), min_duration:0, stall:430380800],
[w:2080, h:1170, format:JPEG(256), min_duration:0, stall:372785600],
[w:2048, h:1536, format:JPEG(256), min_duration:0, stall:423346688],
[w:1920, h:1080, format:JPEG(256), min_duration:0, stall:347225600],
[w:1600, h:1200, format:JPEG(256), min_duration:0, stall:336320000],
[w:1600, h:900, format:JPEG(256), min_duration:0, stall:302240000],
[w:1280, h:720, format:JPEG(256), min_duration:0, stall:265433600],
[w:800, h:600, format:JPEG(256), min_duration:0, stall:234080000],
[w:720, h:480, format:JPEG(256), min_duration:0, stall:224537600],
[w:640, h:480, format:JPEG(256), min_duration:0, stall:221811200],
[w:352, h:288, format:JPEG(256), min_duration:0, stall:207197696],
[w:320, h:240, format:JPEG(256), min_duration:0, stall:205452800],
[w:176, h:144, format:JPEG(256), min_duration:0, stall:201799424],
[w:1920, h:1080, format:PRIVATE(34), min_duration:0, stall:0],
[w:1280, h:960, format:PRIVATE(34), min_duration:0, stall:0],
[w:1280, h:720, format:PRIVATE(34), min_duration:0, stall:0],
[w:800, h:600, format:PRIVATE(34), min_duration:0, stall:0],
[w:720, h:480, format:PRIVATE(34), min_duration:0, stall:0],
[w:640, h:480, format:PRIVATE(34), min_duration:0, stall:0],
[w:352, h:288, format:PRIVATE(34), min_duration:0, stall:0],
[w:320, h:240, format:PRIVATE(34), min_duration:0, stall:0],
[w:176, h:144, format:PRIVATE(34), min_duration:0, stall:0],
[w:1920, h:1080, format:YUV_420_888(35), min_duration:0, stall:0],
[w:1280, h:960, format:YUV_420_888(35), min_duration:0, stall:0],
[w:1280, h:720, format:YUV_420_888(35), min_duration:0, stall:0],
[w:800, h:600, format:YUV_420_888(35), min_duration:0, stall:0],
[w:720, h:480, format:YUV_420_888(35), min_duration:0, stall:0],
[w:640, h:480, format:YUV_420_888(35), min_duration:0, stall:0],
[w:352, h:288, format:YUV_420_888(35), min_duration:0, stall:0],
[w:320, h:240, format:YUV_420_888(35), min_duration:0, stall:0],
[w:176, h:144, format:YUV_420_888(35), min_duration:0, stall:0],
[w:1920, h:1080, format:YV12(842094169), min_duration:0, stall:0],
[w:1280, h:960, format:YV12(842094169), min_duration:0, stall:0],
[w:1280, h:720, format:YV12(842094169), min_duration:0, stall:0],
[w:800, h:600, format:YV12(842094169), min_duration:0, stall:0],
[w:720, h:480, format:YV12(842094169), min_duration:0, stall:0],
[w:640, h:480, format:YV12(842094169), min_duration:0, stall:0],
[w:352, h:288, format:YV12(842094169), min_duration:0, stall:0],
[w:320, h:240, format:YV12(842094169), min_duration:0, stall:0],
[w:176, h:144, format:YV12(842094169), min_duration:0, stall:0]),
HighResolutionOutputs(),
Inputs(),
ValidOutputFormatsForInput(),
HighSpeedVideoConfigurations())
int[] iii = streamConfigurationMap.getOutputFormats();
256
int[] iii = streamConfigurationMap.getOutputFormats();
256
34
35
842094169
2018年7月5日木曜日
android.hardware.camera2, android.view.TextureView
android.hardware.camera2, android.view.TextureView
下面的代碼可以打開相機
package com.kankanla.e560.m180704camera;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.TextureView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 88);
return;
} else {
camera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
camera();
}
protected void camera() {
final CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
final TextureView textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@SuppressLint("MissingPermission")
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
final Surface sss = new Surface(surface);
try {
cameraManager.openCamera("0", new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull final CameraDevice camera) {
try {
camera.createCaptureSession(Arrays.asList(sss), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
CaptureRequest.Builder captureRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequest.addTarget(sss);
session.setRepeatingRequest(captureRequest.build(), new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
}
}
下面的代碼可以打開相機
package com.kankanla.e560.m180704camera;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.TextureView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 88);
return;
} else {
camera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
camera();
}
protected void camera() {
final CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
final TextureView textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@SuppressLint("MissingPermission")
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
final Surface sss = new Surface(surface);
try {
cameraManager.openCamera("0", new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull final CameraDevice camera) {
try {
camera.createCaptureSession(Arrays.asList(sss), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
CaptureRequest.Builder captureRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequest.addTarget(sss);
session.setRepeatingRequest(captureRequest.build(), new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
}
}
2018年7月1日日曜日
android.hardware.camera2
android.hardware.camera2
下面的代碼可以打開相機
package com.kankanla.e560.m180701_camera;
import android.annotation.SuppressLint;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mCamera();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@SuppressLint("MissingPermission")
protected void mCamera() throws CameraAccessException {
SurfaceView surfaceView = findViewById(R.id.surfaceView);
final Surface surface = surfaceView.getHolder().getSurface();
SurfaceHolder surfaceHolder = surfaceView.getHolder();
final CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraManager.openCamera("0", new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull final CameraDevice camera) {
try {
camera.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
CaptureRequest.Builder builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
CaptureRequest captureRequest = builder.build();
session.setRepeatingRequest(captureRequest, new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
}
下面的代碼可以打開相機
package com.kankanla.e560.m180701_camera;
import android.annotation.SuppressLint;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mCamera();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@SuppressLint("MissingPermission")
protected void mCamera() throws CameraAccessException {
SurfaceView surfaceView = findViewById(R.id.surfaceView);
final Surface surface = surfaceView.getHolder().getSurface();
SurfaceHolder surfaceHolder = surfaceView.getHolder();
final CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraManager.openCamera("0", new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull final CameraDevice camera) {
try {
camera.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
CaptureRequest.Builder builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
CaptureRequest captureRequest = builder.build();
session.setRepeatingRequest(captureRequest, new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
}
登録:
投稿 (Atom)