이진화 한 영상을 확인하기 위해서 파일 저장을 했는데 아무 이미지 표시가 안되길래 확인을 해보니
해상도가 -1 x -1 이더군요
하지만 코드상에서 BItmap.getWidth와 getHeight가 각각 1920 과 1080으로 잘 나오고
그 객체를 Bit라 할 때
/*90도 돌리기*/
Matrix mat = new Matrix();
mat.postRotate(90);
Bit = Bit.createBitmap(Bit, 0, 0, Bit.getWidth(), Bit.getHeight(), mat, true);
/* 경로 설정 */
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "test"+String.valueOf(test++)+".png");//test는 int형 변수로 여러개를 번호대로 저장함
FileWriter write = new FileWriter(file, false);
PrintWriter outp = new PrintWriter(write);
outs = new FileOutputStream(file);
Bit.compress(Bitmap.CompressFormat.PNG, 100, outs);
outp.println(Bit);
outp.close();
write.close();
이런식으로 저장합니다.
혹시 몰라 전체 코드 올리겠습니다.
==========================================================
public void Binarization(final Camera mCamera/*, final SurfaceHolder mHolder*/) {
// TODO Read me
// 해상도 문제가 있음.
// 저장은 되지만 -1 x -1 크기
final Camera finalMCamera = mCamera;
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters params = finalMCamera.getParameters();
int w = params.getPreviewSize().width;
int h = params.getPreviewSize().height;
params.setPictureSize(w, h);
mCamera.setParameters(params);
int format = params.getPreviewFormat();
YuvImage image = new YuvImage(data, format, w, h, null);
Log.d("Tag", "w: "+String.valueOf(w)+"\nh: "+String.valueOf(h));
ByteArrayOutputStream out = new ByteArrayOutputStream();
Rect area = new Rect(0, 0, w, h);
image.compressToJpeg(area, 50, out);
Bitmap Bit = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size()).copy(Bitmap.Config.ARGB_8888, true);
Bitmap original = Bit;
for (int i = 0; i < data.length; i += 2) {
if (i % Bit.getWidth() % 2 == 1) continue;
if (i / Bit.getWidth() >= Bit.getHeight()) break;
int pixel = Bit.getPixel(i % Bit.getWidth(), i / Bit.getWidth());
int a = Color.alpha(pixel);
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
if (pixel > 1073741823.5) {
Bit.setPixel(i % Bit.getWidth(), i / Bit.getWidth(), 2147483647);
} else {
Bit.setPixel(i % Bit.getWidth(), i / Bit.getWidth(), 1);
}
}
OutputStream outs = null;
try {
Matrix mat = new Matrix();
mat.postRotate(90);
Bit = Bit.createBitmap(Bit, 0, 0, Bit.getWidth(), Bit.getHeight(), mat, true);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "test"+String.valueOf(test++)+".png");
FileWriter write = new FileWriter(file, false);
PrintWriter outp = new PrintWriter(write);;
outs = new FileOutputStream(file);
Bit.compress(Bitmap.CompressFormat.PNG, 100, outs);
outp.println(Bit);
outp.close();
write.close();
Log.d("Tag", "release");
MediaScanner scanner = MediaScanner.newInstance(MainActivity.this);
scanner.mediaScanning(getApplicationContext().getFilesDir().getPath().toString()+"test.png");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "file error", Toast.LENGTH_LONG).show();
Log.d("Tag", "error");
e.printStackTrace();
} finally {
return;
}
}
});
}