|
|
|
Android ขอความช่วยเหลือ ตัวโค้ดเกิดความผิดพลาด อยากทราบวิธีแก้ไข |
|
|
|
|
|
|
|
เป็นแอพที่ใช้กล้อง ส่องสีแล้วแสดงออกมาเป็นข้อความ
error ตรงบรรทัดที่
v = (0xff & yuv420sp[uvp++]) - 128;
กับ
Point p = DisplayUtil.getScreenMetrics(this);
decodeYUV420SP(pixels, data, p.x, p.y);
แก้ไขยังไงครับผม
Code (Android-Java)
public class CameraActivity extends Activity implements CamOpenOverCallback,Camera.PreviewCallback {
private static final String TAG = "yanzi";
CameraSurfaceView surfaceView = null;
ImageButton shutterBtn;
TextView tvColorHEX;
float previewRate = -1f;
private int[] pixels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread openThread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
CameraInterface.getInstance().doOpenCamera(CameraActivity.this);
}
};
openThread.start();
setContentView(R.layout.activity_camera);
initUI();
initViewParams();
shutterBtn.setOnClickListener(new BtnListeners());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
private void initUI() {
surfaceView = (CameraSurfaceView) findViewById(R.id.camera_surfaceview);
shutterBtn = (ImageButton) findViewById(R.id.btn_shutter);
tvColorHEX = (TextView) findViewById(R.id.tv_color_HEX);
}
private void initViewParams() {
LayoutParams params = surfaceView.getLayoutParams();
Point p = DisplayUtil.getScreenMetrics(this);
params.width = p.x;
params.height = p.y;
previewRate = DisplayUtil.getScreenRate(this);
surfaceView.setLayoutParams(params);
pixels = new int[params.width * params.height];
LayoutParams p2 = shutterBtn.getLayoutParams();
p2.width = DisplayUtil.dip2px(this, 80);
p2.height = DisplayUtil.dip2px(this, 80);
shutterBtn.setLayoutParams(p2);
}
@Override
public void cameraHasOpened() {
// TODO Auto-generated method stub
SurfaceHolder holder = surfaceView.getSurfaceHolder();
CameraInterface.getInstance().setPreviewCallback(this);
CameraInterface.getInstance().doStartPreview(holder, previewRate);
}
private class BtnListeners implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_shutter:
CameraInterface.getInstance().doTakePicture();
break;
default:
break;
}
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (pixels == null) return;
Point p = DisplayUtil.getScreenMetrics(this);
decodeYUV420SP(pixels, data, p.x, p.y);
String a = Integer.toHexString(pixels[pixels.length / 2 + p.y / 2]);
String b1 = a.substring(2, 8);
String c = b1;
int b2 = Integer.parseInt(b1, 16);
if (b2 >= 0xffffff) {
c = "ขาว White";
}
if (b2 >= 0xffff00 && b2 <= 0xffffff) {
c = "เหลือง Yellow";
}
if (b2 >= 0xff00ff && b2 <= 0xffff00) {
c = "ม่วง Violet";
}
if (b2 >= 0xff0000 && b2 <= 0xff00ff) {
c = "แดง Red";
}
if (b2 >= 0x00ffff && b2 <= 0xff0000) {
c = "ฟ้า Lightblue";
}
if (b2 >= 0x00ff00 && b2 <= 0x00ffff) {
c = "เขียว Green";
}
if (b2 >= 0x0000ff && b2 <= 0x00ff00) {
c = "น้ำเงิน Blue";
}
if (b2 >= 0x000000 && b2 <= 0x0000ff) {
c = "สีดำ Black";
}
tvColorHEX.setText(c);
}
void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
//rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
//| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
}
}
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2017-03-04 14:11:38 |
By :
ikkquu789 |
View :
927 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มี LogCat ไหม๊ครับ
|
|
|
|
|
Date :
2017-03-04 22:19:54 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|