Android App kapalıyken Service den kamera görüntüsü alma

FireMonkey ve Mobil uygulama (iOS, Android, Windows Phone) ile ilgili sorularınızı bu foruma sorabilirsiniz.
Cevapla
ekremkocak
Üye
Mesajlar: 21
Kayıt: 31 Oca 2015 03:24

Android App kapalıyken Service den kamera görüntüsü alma

Mesaj gönderen ekremkocak » 08 Eki 2018 12:22

Kod: Tümünü seç

// http://ipadress:port   http:// ipadress:8080

//https://www.youtube.com/watch?v=krIuW71QX7A

// https://www.youtube.com/watch?v=NKrJpU1q1yk

//program calışıyor biraz düzenlemek lazım.

// http serverde MJPEG Stream protokolunu uyguluyor.

unit UntService;

interface

uses
  System.SysUtils,
  System.Classes,
  System.Android.Service,
  AndroidApi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Os, IdBaseComponent, IdComponent, IdCustomTCPServer,
  IdCustomHTTPServer, IdHTTPServer,
  android.hardware.Camera, Androidapi.JNI.JavaTypes,
  Androidapi.JNIBridge,
  Androidapi.Helpers,
  Androidapi.JNI.App,
  System.Messaging,
  System.SyncObjs,IdGlobal,IdContext,
  FastUtils,
  Androidapi.JNI.Hardware;



type
  TAndroidVideoCaptureCallback = class(TJavaLocal, JCamera_PreviewCallback)
  private
  public
    procedure onPreviewFrame(AData: TJavaArray<Byte>; ACamera: JCamera); cdecl;
  end;

type
  TfrmService = class(TAndroidService)
    IdHTTPServer1: TIdHTTPServer;
    function AndroidServiceStartCommand(const Sender: TObject;
      const Intent: JIntent; Flags, StartId: Integer): Integer;
    procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    procedure IdHTTPServer1Connect(AContext: TIdContext);
    procedure IdHTTPServer1Disconnect(AContext: TIdContext);
  private
    { Private declarations }
  public
    { Public declarations }
    class var FCallback: TAndroidVideoCaptureCallback;
  end;

var
  frmService: TfrmService;
  Camera: JCamera;
  Params: JCamera_Parameters;
  PreviewSize: JCamera_Size;
  Bitmap: JBitmap;
implementation

{%CLASSGROUP 'FMX.Controls.TControl'}

{$R *.dfm}

procedure  decodeYUV420SP(var rgb:TJavaArray<Integer>; yuv420sp:TJavaArray<byte>;  width,  height:Integer);
var
  frameSize:Integer;
  j,yp,uvp,u,v,i,y,y1192,r,g,b :Integer;
begin
	 frameSize := width * height;
     yp := 0;
		for  j := 0 to  height-1 do begin

      uvp := frameSize + (j shr 1) * width;
       u := 0;
       v := 0;
			for  i := 0 to  width-1 do begin

			  y := ($ff and (Integer( yuv420sp.Items[yp]))) - 16;
				if (y < 0) then y := 0;
				if ((i and 1) = 0) then begin
					v := ($ff and yuv420sp.Items[uvp]) - 128; Inc(uvp);
					u := ($ff and yuv420sp.Items[uvp]) - 128; Inc(uvp);
				end;

				 y1192 := 1192 * y;
				r := (y1192 + 1634 * v);
				g := (y1192 - 833 * v - 400 * u);
				 b := (y1192 + 2066 * u);

				if (r < 0) then r := 0 else if (r > 262143) then r := 262143;
				if (g < 0) then g := 0 else if (g > 262143) then g := 262143;
				if (b < 0) then b := 0 else if (b > 262143) then b := 262143;

				rgb[yp] := $ff000000 or ((r shl 6) and $ff0000) or ((g shr 2) and $ff00) or ((b shr 10) and $ff);
        Inc(yp);

			end;
		end;
	end;

 
procedure TAndroidVideoCaptureCallback.onPreviewFrame(AData: TJavaArray<Byte>;
  ACamera: JCamera);
var
   RGBData:TJavaArray<Integer>;
begin

  RGBData := TJavaArray<Integer>.Create(PreviewSize.width* PreviewSize.height);
  decodeYUV420SP(RGBData, AData,PreviewSize.width,PreviewSize.height);
  Bitmap.setPixels(RGBData, 0, PreviewSize.width, 0, 0,PreviewSize.width,PreviewSize.height);
  ACamera.addCallbackBuffer(AData);

end;




function TfrmService.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
  Result := TJService.JavaClass.START_STICKY;
end;


procedure TfrmService.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
 var
   Strm: TMemoryStream;
   JStream: JByteArrayOutputStream;
begin



    if Bitmap<>nil  then
    begin

          
            AContext.Connection.IOHandler.WriteLn('HTTP/1.0 200 OK');
            AContext.Connection.IOHandler.WriteLn('Connection: close');
            AContext.Connection.IOHandler.WriteLn('Max-Age: 0');
            AContext.Connection.IOHandler.WriteLn('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0');
            AContext.Connection.IOHandler.WriteLn('Pragma: no-cache');
            AContext.Connection.IOHandler.WriteLn('Content-Type: multipart/x-mixed-replace; boundary=--imgboundary');
            AContext.Connection.IOHandler.WriteLn;



                 while AContext.Connection.Connected do
                  begin
                     Strm := TMemoryStream.Create;
                     try
                       Strm.Clear;
                       JStream := TJByteArrayOutputStream.JavaClass.init(0);
                       Bitmap.compress( TJBitmap_CompressFormat.JavaClass.JPEG, 50,JStream);

                       Strm.WriteBuffer(JStream.toByteArray.Data^,JStream.size);
                       Strm.Position:=0;
                       AContext.Connection.IOHandler.WriteLn('--imgboundary');
                       AContext.Connection.IOHandler.WriteLn('Content-type: image/jpeg');
                       AContext.Connection.IOHandler.WriteLn('Content-Length: ' + IntToStr(Strm.size));
                       AContext.Connection.IOHandler.WriteLn;
                       AContext.Connection.IOHandler.Write(Strm);
                       AContext.Connection.IOHandler.WriteLn;

                       JStream.close;
                     finally
                       Strm.Free;
                     end;


            
                 end;

    end;

end;

procedure TfrmService.IdHTTPServer1Connect(AContext: TIdContext);
begin
       Camera := TJCamera.JavaClass.open(0);   /// open(0); ön kamera  open(1) arka kamera
       if Camera <> nil then begin
         Params := Camera.getParameters;
         Params.setPreviewFormat(TJImageFormat.JavaClass.YV12);
         Camera.setParameters(Params);
         PreviewSize := Params.getPreviewSize;
         Bitmap := TJBitmap.JavaClass.createBitmap(PreviewSize.width, PreviewSize.height, TJBitmap_Config.JavaClass.RGB_565);
         FCallback := TAndroidVideoCaptureCallback.Create;
         Camera.setPreviewCallback(FCallback );
         Camera.startPreview;
       end;
end;

procedure TfrmService.IdHTTPServer1Disconnect(AContext: TIdContext);
begin
  Camera.stopPreview;
  Camera.release;

end;

end.

ekremkocak
Üye
Mesajlar: 21
Kayıt: 31 Oca 2015 03:24

Re: Android App kapalıyken Service den kamera görüntüsü alma

Mesaj gönderen ekremkocak » 19 Eki 2018 10:04

Android Services AutoRun Calışması.

Kod: Tümünü seç

public class BootCompletedReceiver extends BroadcastReceiver {

	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			Intent TestLauncher = new Intent();
            TestLauncher.setClassName(context, "com.embarcadero.services.CamService");
			context.startService(TestLauncher);
		}

	}
 
}

Cevapla